19/01
2017
Wallogit.com
2017 © Pedro Peláez
JSON exceptions for your API
Convert your Lumen exceptions into JSON:, (*1)
{
"code": nnn,
"message": "a message",
"description": "a description",
"errors": [
"error 1",
"error 2",
...
]
}
Set your composer.json to allow less stable packages:, (*2)
"minimum-stability" : "dev", "prefer-stable" : true
Require the package as usual:, (*3)
composer require nextdots/api-exceptions
Change app/Exceptions/Handler.php:, (*4)
Change this:, (*5)
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
To this:, (*6)
use ApiExceptions\Handler as ExceptionHandler;
On Linux, you can do the same as above with the following command:, (*7)
sed -i 's/Laravel\\Lumen\\Exceptions\\Handler/ApiExceptions\\Handler/' app/Exceptions/Handler.php
use ApiExceptions\JsonResponseException;
$app->get('/throw-exception', function () {
throw new JsonResponseException(400, "an exception", "a description");
});