PrStandup
PrStandup

Reputation: 343

How to catch all exceptions in Laravel

I was wondering if there is a way to catch all the exceptions that are thrown in a laravel app and store them in database ?

I have been looking at some packages but coudn't find anything that tells where and how to catch the exceptions.

Upvotes: 6

Views: 17183

Answers (2)

Sapnesh Naik
Sapnesh Naik

Reputation: 11636

As stated in the Docs, You need to customize the render() method of App\Exceptions\Handler.

Edit the app/Exceptions/Handler.php:

public function render($request, Exception $e)
{
    $error =$e->getMessage();

    //do your stuff with the error message 

    return parent::render($request, $exception);
}

Upvotes: 5

Milad Teimouri
Milad Teimouri

Reputation: 1211

for catch all errors and log them you need to edit App\Exceptions\Handler file like this

 public function render($request, Exception $exception)
{
    if ($exception){
        // log the error
        return response()->json([
            'status' => $exception->getStatusCode(),
            'error' => $exception->getMessage()
        ]);
   }
    return parent::render($request, $exception);
}

Upvotes: 14

Related Questions