Download the PHP package sfelix-martins/json-exception-handler without Composer
On this page you can find all versions of the php package sfelix-martins/json-exception-handler. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sfelix-martins/json-exception-handler
More information about sfelix-martins/json-exception-handler
Files in sfelix-martins/json-exception-handler
Package json-exception-handler
Short Description Adds more power to Laravel Exceptions Handler to treat json responses
License MIT
Informations about the package json-exception-handler
Laravel Json Exception Handler
Adds methods to your App\Exceptions\Handler
to treat json responses.
It is most useful if you are building APIs!
Requirements
- Laravel Framework >= 5.4
- php >= 7.0
JsonAPI
Using JsonAPI standard to responses!
Features
Default error response:
To Illuminate\Validation\ValidationException
:
Treated Exceptions
Illuminate\Auth\Access\AuthorizationException
Illuminate\Auth\AuthenticationException
Illuminate\Database\Eloquent\ModelNotFoundException
Illuminate\Validation\ValidationException
Laravel\Passport\Exceptions\MissingScopeException
League\OAuth2\Server\Exception\OAuthServerException
Symfony\Component\HttpKernel\Exception\NotFoundHttpException
Symfony\Component\HttpKernel\Exception\BadRequestHttpException
Installing and configuring
Install the package
If you are not using Laravel 5.5 version add the JsonHandlerServiceProvider
to your config/app.php
providers array:
Publish the config to set your own exception codes
Set your exception codes on config/json-exception-handler.php
on codes array.
You can add more fields and codes to validation_fields
array.
You can add too your models on lang packages to return the Not Found response translated correctly.
In resources/lang/vendor/exception/lang/$locale
in exceptions
file you can set on models
array. Example:
Using
Use the trait on your App\Exception\Handler
and add method jsonResponse()
passing the $exception
if $request
expects a json response on render()
method
Use sample
Extending
You can too create your own handler to any Exception. E.g.:
-
Create a Handler class that extends of
AbstractHandler
: -
You must implements the method
handle()
fromAbstractHandler
class. The method must return an instance ofError
orErrorCollection
: - Now just registry your customized handler on
App\Exception\Handler
file on attributeexceptionHandlers
. E.g: