Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

283
Views
Laravel custom error taking too long to response

Laravel Framework 8.48.0
PHP 7.4.18
Local Environment: Xampp, Windows10

I am developing RESTful API using Laravel 8. I have written some logic inside the register() method to make the error response better and more straightforward. Everything is working perfectly. But the problem is when Laravel detects an exception that doesn't fall any of this logic and APP_DEBUG=true, this piece of code start executing and takes too long to respond; it says execution timeout. If I clean the register() method, it shows an exception within 1 or 2 secs. What's wrong with my code?

I want a better and more precise error response + when APP_DEBUG=true quickly shows an error exception.

I think this is the culprit.

if (env('APP_DEBUG', false)) {
  return parent::render($request, $exception);
}

Handler.php

class Handler extends ExceptionHandler
{
    use ApiResponser;

    protected $dontReport = [
        //
    ];
    
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];
    
    public function register()
    {
        $this->renderable(function (Exception $exception, $request) {
            if ($request->wantsJson()) {

                if ($exception instanceof HttpException) {
                    $code = $exception->getStatusCode();
                    $message = Response::$statusTexts[$code];

                    return $this->errorResponse($message, $code);
                }

                if ($exception instanceof AuthenticationException) {
                    return $this->errorResponse($exception->getMessage(), Response::HTTP_UNAUTHORIZED);
                }

                if ($exception instanceof AuthorizationException) {
                    dd('authorization exception');
                    return $this->errorResponse($exception->getMessage(), Response::HTTP_FORBIDDEN);
                }

                if ($exception instanceof ValidationException) {
                    $errors = $exception->validator->errors()->getMessages();

                    return $this->errorResponse($errors, Response::HTTP_UNPROCESSABLE_ENTITY);
                }
                
                if (env('APP_DEBUG', false)) {
                    return parent::render($request, $exception);
                }

                return $this->errorResponse('Unexpected error occurred. Please contact your administrator for help.', Response::HTTP_INTERNAL_SERVER_ERROR);
            }
        });
    }
}
over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!