PHPackages                             ninhtqse/heimdal - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. ninhtqse/heimdal

ActiveLibrary

ninhtqse/heimdal
================

11.0kPHP

Since Dec 15Pushed 5y ago1 watchersCompare

[ Source](https://github.com/ninhtqse/heimdal)[ Packagist](https://packagist.org/packages/ninhtqse/heimdal)[ RSS](/packages/ninhtqse-heimdal/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

Heimdal
=======

[](#heimdal)

[![Latest Version](https://camo.githubusercontent.com/422c11641514068421c82b37658c973cd799c52fcfcff7ec7e2e0bdf282986aa/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f657362656e702f6865696d64616c2e7376673f7374796c653d666c61742d737175617265)](https://github.com/esbenp/heimdal/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Build Status](https://camo.githubusercontent.com/c774b78911fdb63e623a4ff6d620b740a74d4d5477fce5343b023b784d0c4c7d/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f657362656e702f6865696d64616c2f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/esbenp/heimdal)[![Coverage Status](https://camo.githubusercontent.com/b750ed7f924599e0bc52fea4ca3c73f3ed32cd583bdb8ed53d3a3dbb122dbb95/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f657362656e702f6865696d64616c2e7376673f7374796c653d666c61742d737175617265)](https://coveralls.io/github/esbenp/heimdal)[![Total Downloads](https://camo.githubusercontent.com/5e9797415693a9f2fb861cc033964f167526a065b49e843cdb7ba9dba58a21c8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6f7074696d75732f6865696d64616c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/optimus/heimdal)

Introduction
------------

[](#introduction)

Heimdal is a Laravel exception handler build specifically for APIs.

### Why is it needed?

[](#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](https://jsonapi.org)have [guidelines for how errors should be formatted](http://jsonapi.org/format/#error-objects).

Installation
------------

[](#installation)

```
composer require optimus/heimdal ~1.0
```

Add the service provider to `config/app.php`

```
// other providers...
Optimus\Heimdal\Provider\LaravelServiceProvider::class,

```

Publish the configuration.

```
php artisan vendor:publish --provider="Optimus\Heimdal\Provider\LaravelServiceProvider"

```

Add the exception handler to `bootstrap/app.php`

```
$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    Optimus\Heimdal\ExceptionHandler::class
);

```

What does it do?
----------------

[](#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.

```
1. Exception is thrown
2. The Exception is parsed through reports. A reporter is a class that reports the Exception. Log it in logs, send to external trackers like Sentry, Bugsnag etc.
3. The Exception is parsed through an appropriate formatter that formats the response in accordance to the error type.
4. The response is sent to the user.

```

### Is this not what Laravel does?

[](#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](https://github.com/Seldaek/monolog/issues/651). 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
-------------

[](#configuration)

Heimdal has two things that should be configured: formatters and reporters.

### 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`

```
'sentry' => [
    'class'  => \Optimus\Heimdal\Reporters\SentryReporter::class,
    'config' => [
        'dsn' => '',
        // For extra options see https://docs.sentry.io/clients/php/config/
        // php version and environment are automatically added.
        'sentry_options' => []
    ]
]
```

Adding a custom reporter, for instance Bugsnag, is as simple as writing a small reporter class like this

```
