outrowender
outrowender

Reputation: 401

is there a clean way to intercept *handled* errors in Laravel?

Im trying intercept errors in laravel and i found a nice way to do that:

Simulating a error:

public function index(){       

 $users = User::all(); //<-SQL exeception here             

 return response()->json(['message'=>'ok'], 200);
}

app/Exceptions/Handler.php

public function report(Exception $exception)
{
    dd($exception); //<-intercept my error here
    parent::report($exception);
}

Works very well and i can do whatever i want with error, but when i use a try-catch block, my interceptor does not work:

Simulating error again

public function index(){

 try {

   $users = User::all();//<-SQL exeception here 

 } catch (\Throwable $th) {

   error_log('Error handled');

   //MyInterceptor::manuallyIntercept($th);

 }        

    return response()->json(['message'=>'ok'], 200);
}

Is there a clean way to intercept all handled errors programatically?

Upvotes: 0

Views: 1529

Answers (1)

BugraDayi
BugraDayi

Reputation: 538

Not report method, you need to use render method on Handler.php You will see $this->errorResponse which is to just return JSON response. I just want to show the main idea.

public function render($request, Exception $exception)
{
    if ($exception instanceof ValidationException) {
        return $this->convertValidationExceptionToResponse($exception, $request);
    }
    if ($exception instanceof ModelNotFoundException) {
        $modelName = strtolower(class_basename($exception->getModel()));
        return $this->errorResponse("Does not exists any {$modelName} with the specified identificator", 404);
    }
    if ($exception instanceof AuthenticationException) {
        return $this->unauthenticated($request, $exception);
    }
    if ($exception instanceof AuthorizationException) {
        return $this->errorResponse($exception->getMessage(), 403);
    }
    if ($exception instanceof MethodNotAllowedHttpException) {
        return $this->errorResponse('The specified method for the request is invalid', 405);
    }
    if ($exception instanceof NotFoundHttpException) {
        return $this->errorResponse('The specified URL cannot be found', 404);
    }
    if ($exception instanceof HttpException) {
        return $this->errorResponse($exception->getMessage(), $exception->getStatusCode());
    }
    if ($exception instanceof QueryException) {
        $errorCode = $exception->errorInfo[1];
        if ($errorCode == 1451) {
            return $this->errorResponse('Cannot remove this resource permanently. It is related with any other resource', 409);
        }
    }
    if (config('app.debug')) {
        return parent::render($request, $exception);
    }
    return $this->errorResponse('Unexpected Exception. Try later', 500);
}

Error response method

protected function errorResponse($message, $code)
    {
        return response()->json(['error' => $message, 'code' => $code], $code);
    }

Upvotes: 1

Related Questions