PHPackages                             elsayed85/laravel-api-exceptions - 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. [Framework](/categories/framework)
4. /
5. elsayed85/laravel-api-exceptions

ActiveLibrary[Framework](/categories/framework)

elsayed85/laravel-api-exceptions
================================

All in one solution for exception for JSON REST APIs on Laravel and Lumen.

3.4.0(4y ago)027MITPHPPHP ^7.2|^8.0

Since Jun 30Pushed 3y agoCompare

[ Source](https://github.com/elsayed85/laravel-api-exceptions)[ Packagist](https://packagist.org/packages/elsayed85/laravel-api-exceptions)[ RSS](/packages/elsayed85-laravel-api-exceptions/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (25)Used By (0)

Laravel-API-Exceptions
======================

[](#laravel-api-exceptions)

[![Travis](https://camo.githubusercontent.com/93e9bdc997124001df0d8bf7f2dc91869204ec9f36382397ecdf5eddb4db9b37/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f727573742d6c616e672f727573742e737667)](https://travis-ci.org/mlanin/laravel-api-exceptions)

> All in one solution for exception for JSON REST APIs on Laravel and Lumen.

About
-----

[](#about)

The goal of this package is to provide you with a set of most common exceptions that may be needed while developing JSON REST API. It also:

- Handles exceptions output.
- Handles exceptions report to logs.
- Overwrites default Validator to make validation errors more verbose.
- Has a FormRequest that to handle validation errors and pass them to ApiExceptions layer.

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

[](#installation)

[PHP](https://php.net) 5.4+ or [HHVM](http://hhvm.com) 3.3+, [Composer](https://getcomposer.org) and [Laravel](http://laravel.com) 5.1+ are required.

To get the latest version of Laravel Laravel-API-Debugger, simply add the following line to the require block of your `composer.json` file.

For Laravel 5.1

```
"lanin/laravel-api-exceptions": "^0.1.0"

```

For Laravel 5.3

```
"lanin/laravel-api-exceptions": "^1.0.0"

```

You'll then need to run `composer install` or `composer update` to download it and have the autoloader updated.

Once Laravel-API-Exceptions is installed, if you're using Laravel 5.1, 5.2, 5.3, and 5.4, you need to register the service provider. Open up `config/app.php` and add the following to the providers key.

```
Lanin\Laravel\ApiExceptions\ApiExceptionsServiceProvider::class,
```

If you're using Laravel 5.5, you don't need to add anything in `config/app.php`. It will use package auto discovery feature in Laravel 5.5.

### Exceptions

[](#exceptions)

Every ApiException can be thrown as a normal exception and they will be automatically serialized to JSON with corresponding HTTP status, if user wants json:

```
{
    "id": "not_found",
    "message": "Requested object not found."
}
```

This object will be also populated with trace info, when `APP_DEBUG` is true.

Also it can have `meta` attribute when there is additional info. For example for validation errors:

```
{
	"id": "validation_failed",
	"message": "Validation failed.",
	"meta": {
		"errors": {
			"tags": [{
				"rule": "max.array",
				"message": "The tags may not have more than 10 items.",
				"parameters": ["10"]
			}]
		}
	}
}
```

For `ValidationApiException`, meta attribute has `errors` object that contains validations errors. Every attribute of this object is a name of a request parameter to validate to and value is an array of errors with description.

#### Customization

[](#customization)

You can customize errors APU response by overriding `formatApiResponse` method in your ExceptionsHandler.

For example if you want to put everything under `error` attribute, you can do it like this:

```
/**
 * Format error message for API response.
 *
 * @param  ApiException  $exception
 * @return mixed
 */
protected function formatApiResponse(ApiException $exception)
{
    return [
        'error' => $exception->toArray(),
    ];
}
```

### Handler

[](#handler)

Extend your default exceptions handler with:

- `\Lanin\Laravel\ApiExceptions\LaravelExceptionHandler` for Laravel
- `\Lanin\Laravel\ApiExceptions\LumenExceptionHandler` for Lumen

And remove everything else. Example:

```
