Download the PHP package digitalroll/heimdal without Composer
On this page you can find all versions of the php package digitalroll/heimdal. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package heimdal
Heimdal
Introduction
Heimdal is a Laravel exception handler build specifically for APIs.
Why is it needed?
When building APIs there are specific formatting do's and dont's on how to send errors back to the user. Frameworks like Laravel are not build specifically for API builders. This small library just bridges that gap. For instance, specifications like JSON API have guidelines for how errors should be formatted.
Installation
Add the service provider to config/app.php
Publish the configuration.
Add the exception handler to bootstrap/app.php
What does it do?
Imagine you have a piece of code that throws an InvalidArgumentException
. This is a server error (500). It will parse through the flow
described below.
Is this not what Laravel does?
Yes, pretty much. However, if you want to report to something like Sentry you usually do this through something like Monolog. The problem with Monolog is that it is difficult to pick up the response of the reporters. For instance, Sentry reports back a unique ID for every reported exception which is extremely useful to give to the user, so they can give it to customer support. Heimdal supports this out-of-the-box by giving the response of all reporters to the formatter classes. This makes it trivial for formatters to use the response of the reporters in their final response to the user.
Second, Heimdal comes with sensible defaults as how different error types should be reported to the user. And makes it trivial to implement alternative responses for specific exception types.
Configuration
Heimdal has two things that should be configured: formatters and reporters.
Reporters
You should determine where your exceptions should be reported to. Heimdal still calls the base report function in Laravel, so your
exceptions will still be logged as normal. However, adding external reporting is easy. Heimdal comes with Sentry integration out of the box.
To send exceptions to Sentry simply add this entry to the reporters
section in config/optimus.heimdal.php
Adding a custom reporter, for instance Bugsnag, is as simple as writing a small reporter class like this
And then add it to config/optimus.heimdal.php
Formatters
Heimdal already comes with sensible formatters out of the box. In config/optimus.heimdal.php
is a section where
the formatter priority is defined.
The higher the entry, the higher the priority. In this example, a UnprocessableEntityHttpException
will be formatted used the
UnprocessableEntityHttpExceptionFormatter
because it is the first entry. However, an NotFoundHttpException
will not match
UnprocessableEntityHttpException
but will match HttpException
(since it is a child class hereof) and will therefore
be formatted using the HttpExceptionFormatter
.
You can write custom formatters easily:
Notice how easily we used $reporterResponses
to attach the ID of the Sentry log to the JSON response.
Now we simply add it to config/optimus.heimdal.php
Now all NotFoundHttpException
s will be formatted using our custom formatter.
Available Reporters
Sentry
To send Exceptions to Sentry add the following reporter configuration in config/optimus.heimdal.php
.
Adding context at runtime
Sometimes you want to add information at runtime, like request data, user information or similar.
For this you can add the add_context
key to the config
array. Below is an example of how it could be implemented.
Bugsnag
Thanks to Nikolaj Løvenhardt Petersen for adding support
Install Bugsnag using the Laravel installation guide
To send Exceptions to Bugsnag add the following reporter configuration in config/optimus.heimdal.php
.
Rollbar
To send Exceptions to Rollbar add the following reporter configuration in config/optimus.heimdal.php
.
The complete list of config options can be found in here
Standards
This package is compliant with PSR-1, PSR-2 and PSR-4. If you notice compliance oversights, please send a patch via pull request.
Testing
Contributing
Please see CONTRIBUTING for details.
License
The MIT License (MIT). Please see License File for more information.