Package Data | |
---|---|
Maintainer Username: | phuocnt0612 |
Maintainer Contact: | phuocnt0612@gmail.com (Phuoc Nguyen) |
Package Create Date: | 2017-09-10 |
Package Last Update: | 2017-09-11 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2025-02-06 03:08:21 |
Package Statistics | |
---|---|
Total Downloads: | 42 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 14 |
Total Watchers: | 3 |
Total Forks: | 2 |
Total Open Issues: | 0 |
This is a Laravel exception handler build specifically for APIs.
Get idea from Heimdal
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 have guidelines for how errors should be formatted.
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
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 class extend Phuocnt\LaravelException\Exceptions\CustomException
class
You may want to define default $statusCode
or render
method which helps to define your custom response
<?php
namespace App\Exceptions\YourExceptions;
use Phuocnt\LaravelException\Exceptions\CustomException as BaseCustomException;
use Symfony\Component\HttpFoundation\Response;
class UnprocessableEntityHttpException extends BaseCustomException
{
protected $statusCode = Response::HTTP_UNPROCESSABLE_ENTITY;
public function __construct(\Exception $e)
{
parent::__construct($e);
}
pubic function render($request, $data = null) {
$data = [
'firstLine' => 'firstLine',
'secondLine' => 'secondLine',
];
return response()->json()->setStatusCode($this->statusCode)->setData($data);
}
}
Now we simply add it to config/exception.php
// Has to be in prioritized order, e.g. highest priority first.
'map' => [
\Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException::class
=> App\Exceptions\YourExceptions\UnprocessableEntityHttpException::class,
AuthenticationException::class => CustomException\AuthenticationException::class,
AuthorizationException::class => CustomException\AuthorizationException::class,
ValidationException::class => CustomException\ValidationException::class,
Exception::class => CustomException\Exception::class,
],
Now all UnprocessableEntityHttpException
s will be formatted using our custom exception.
Write your exception as usual
Override $statusCode
if needed else it will be get in order: this exception class
> your custom exception format
> default laravel status code for this exception
> 500
You can also override render
method here, too
<?php
namespace App\Exceptions;
class SomeException extends \Exception
{
public $statusCode = 400;
public function __construct($message = "Oops, exception's thrown", $code = 99999)
{
parent::__construct(
$message,
$code
);
}
}
In production, you may want to turn APP_DEBUG=false
in .env
.
Almost common errors will have response below with appropriate status code
{
"message": "An error occurred."
}
Status code: 422
{
"errors": {
"id": [
"The id field is required."
],
"name": [
"The name field is required."
]
}
}
{
"message": "This action is unauthorized.",
"line": 165,
"file": "/var/www/lar55/vendor/laravel/framework/src/Illuminate/Foundation/Http/FormRequest.php",
"code": 0,
"exception": "Illuminate\\Auth\\Access\\AuthorizationException: This action is unauthorized. in /var..."
}
The MIT License (MIT). Please see License File for more information.