PHPackages                             phuoc/laravel-exception - 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. phuoc/laravel-exception

ActiveLibrary

phuoc/laravel-exception
=======================

A small package to help handle custom exception from laravel 5.5

1.0.0(8y ago)14432MITPHP

Since Sep 10Pushed 8y ago2 watchersCompare

[ Source](https://github.com/phuocnt0612/laravel-exception)[ Packagist](https://packagist.org/packages/phuoc/laravel-exception)[ RSS](/packages/phuoc-laravel-exception/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

Laravel 5.5 custom exception
============================

[](#laravel-55-custom-exception)

[![Latest Version](https://camo.githubusercontent.com/a96d3bde95c9212f68b92dc24de8f9bdc8526deff0a56a2fe55de304422c340e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f7068756f636e74303631322f6c61726176656c2d657863657074696f6e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/phuocnt0612/laravel-exception/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)

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

[](#introduction)

This is a Laravel exception handler build specifically for APIs.

Get idea from [Heimdal](https://github.com/esbenp/heimdal)

### 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 phuoc/laravel-exception
```

Add the service provider to `config/app.php` if you've disabled `package auto-discovery` feature

```
// other providers...
Phuocnt\LaravelException\Providers\LaravelServiceProvider::class,

```

Publish the configuration.

```
php artisan vendor:publish --provider="Phuocnt\LaravelException\Providers\LaravelServiceProvider"

```

Change `App\Exceptions\Handler`'s extends class

```
namespace App\Exceptions;

use Exception;
use Phuocnt\LaravelException\ExceptionHandler;

class Handler extends ExceptionHandler {
    ...
}

```

Clear cache if it need to

```
php artisan cache:clear

```

Autoload

```
composer dump-autoload

```

Configuration
-------------

[](#configuration)

### Exceptions

[](#exceptions)

This package already comes with sensible custom exceptions out of the box. In `config/exception.php` is a section where the exception priority is defined.

```
    // Has to be in prioritized order, e.g. highest priority first.
    'map' => [
        AuthenticationException::class => CustomException\AuthenticationException::class,
        AuthorizationException::class => CustomException\AuthorizationException::class,
        ValidationException::class => CustomException\ValidationException::class,
        Exception::class => CustomException\Exception::class,
    ],
```

The higher the entry, the higher the priority. In this example, a `AuthenticationException` will be formatted used the `CustomException\AuthenticationException` because it is the first entry. However, an `NotFoundHttpException` will not match `AuthenticationException` but will match `Exception` (since it is a child class hereof) and will therefore be formatted using the `Exception`.

### Write your custom exception easily

[](#write-your-custom-exception-easily)

Write your custom exception class extend `Phuocnt\LaravelException\Exceptions\CustomException` class

You may want to define default `$statusCode` or `render` method which helps to define your custom response

```
