'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);
            }
        });
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source