experus / exceptions
The way to handle exceptions in Laravel.
Requires
- php: >=5.5.9
- filp/whoops: ^2.0@dev
- illuminate/http: ^5.2
This package is not auto-updated.
Last update: 2020-03-09 21:18:44 UTC
README
How to deal with exceptions in bigger Laravel applications.
Installation
- Add this package to your project.
composer require experus/exceptions
- Include the
\Experus\Exceptions\Dispatcher
trait in your\App\Exceptions\Handler
class and remove therender
method
<?php namespace App\Exceptions; use Experus\Exceptions\Dispatcher as DispatchesExceptions; class Handler extends ExceptionHandler { use DispatchesExceptions; // Rest of the handler class }
- Now add a property called
$catchers
to your class. This is a mapping of your handlers and what exceptions they handle.
<?php namespace App\Exceptions; use Experus\Exceptions\Dispatcher as DispatchesExceptions; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; class Handler extends ExceptionHandler { use DispatchesExceptions; private $catchers = [ // A handler can handle multiple exceptions \Foo\Bar\NotFoundHandler::class => [ \Foo\Bar\UserNotFoundException::class, \Foo\Bar\ProductNotFoundException::class, ], // Or just one, in which case you don't need an array. It just works \Foo\Bar\RuntimeException::class => \Foo\Bar\RuntimeHandler::class, ]; // Rest of the handler class }
Writing handlers
When you got all this set up, you have to write the handlers to do something with your exceptions. Fortunately, writing handlers is really easy. All you need is a handle method that accepts a \Illuminate\Http\Request and the exception you're handling. The handle method expects you to return a Response, as demonstrated below:
<?php namespace \Foo\Bar\Exceptions; use Illuminate\Http\Request; class HttpHandler { public function handle(Request $request, \Foo\Bar\DummyException $exception) { return response()->json([ 'status' => $exception->getMessage(), ], 500); } }
What if I have an application that handles both JSON responses and normal requests (views etc)?
Fortunately, we thought of that too. rather than letting you check for yourself all the time we provide the \Experus\Exceptions\Catchers\Catcher
class which does that for you.
Simply extend from \Experus\Exceptions\Catchers\Catcher
and implement the handleWeb
method for normal responses and handleJson
method for your API responses.
Ignoring certain exceptions
Sometimes you may want to let Laravel handle an exception instead,
for example the AuthenticationException
thrown by the auth
middleware in the laravel framework
will by default be handled differently (see the unauthenticated
method on the default handler).
You can simply tell exceptions to ignore these exceptions by defining a blacklist
property:
protected $blacklist = [ \Illuminate\Auth\AuthenticationException::class, ];
In the above example, exceptions will ignore all AuthenticationException
thrown and pass them to the default Laravel exception handler.
Unhandled exceptions
Sometimes your application will encounter an unexpected exception. exceptions ships with the amazing filp/whoops package (which Laravel used to ship by default with). In development exceptions will render using whoops if you do not specify a handler for it, in production we'll delegate your exceptions to the default Laravel exception handler for you.