Download the PHP package pedrosalpr/laravel-api-problem without Composer
On this page you can find all versions of the php package pedrosalpr/laravel-api-problem. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download pedrosalpr/laravel-api-problem
More information about pedrosalpr/laravel-api-problem
Files in pedrosalpr/laravel-api-problem
Package laravel-api-problem
Short Description This is my package laravel-api-problem
License MIT
Homepage https://github.com/pedrosalpr/laravel-api-problem
Informations about the package laravel-api-problem
Laravel Api Problem
The objective of this package is to facilitate error outputs from API requests in accordance with the RFC 9457 standard.
It transforms error outputs into json format with the following characteristics:
- header:
Content-Type: application/problem+json
- response:
type
: URI that identifies the type of error that occured, for examplehttps://example.com/validation-error
.title
: Human-readable identifier, usually the same type field should have the same title field alongside. An example would be something like Form validation failed.status
: A copy of the HTTP status code.detail
: More information about the specific problem, and if it's appropriate also steps to correct it. For example information about a form validation problem Username is already taken, please choose a different username.instance
: An identifier for this specific occurence, which may not be useful to the client but may be included in a bug report for example.- Additional fields: Any fields may be added to give additional information, and consuming clients are expected to ignore any that they don't have specific support for.
timestamp
: (RFC 9457 enhancement): A timestamp indicating when the problem was generated. This helps in logging and tracing the error, especially in systems where timing is critical.
Example:
Server Response:
Installation
You can install the package via composer:
Usage
Default Mode (Older Version Laravel 9 and 10)
To use it, just go to the register
method within Exceptions\Handler.php
and add the following code:
If you want to debug, just add the following line before the return:
dd($apiProblem->toDebuggableArray());
Default Mode (Laravel 11)
To use it, add the following code to the Exception Closure in the bootstrap/app.php
file:
Creating Exceptions in the Api Problem pattern
There is the possibility of creating exceptions that extend LaravelApiProblemException
.
This already makes it easier to transform the exception into the Api Problem pattern.
To do this, simply run the following command:
php artisan laravel-api-problem:exception {name}
For example:
php artisan laravel-api-problem:exception DummyException
This creates a class exception inside Exceptions\ApiProblem
;
Custom Mode
If you want to customize an Api Problem
class to add your guidelines for which error responses should be returned, simply extend the class with the following command:
php artisan laravel-api-problem:extend
For example:
php artisan laravel-api-problem:extend DummyApiProblem
This creates a class Api Problem inside ApiProblem
;
And within the match, add the names of the exceptions classes with their respective methods, such as dummy()
.
Older Version Laravel 9 and 10
In the Handler.php
file replace the LaravelApiProblem
object instance to DummyApiProblem
.
Laravel 11
Add the following code to the Exception Closure in the bootstrap/app.php
file:
Testing
TODO
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Leandro Pedrosa Rodrigues
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-api-problem with dependencies
illuminate/contracts Version ^10.0||^11
spatie/laravel-package-tools Version ^1.14.0