Download the PHP package vaened/laravception without Composer
On this page you can find all versions of the php package vaened/laravception. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download vaened/laravception
More information about vaened/laravception
Files in vaened/laravception
Package laravception
Short Description Standardize your error responses in Laravel with Laravception. Get consistent error codes, messages, metadata, and translation support.
License MIT
Informations about the package laravception
Laravception
Laravception standardizes error responses in Laravel, ensuring all exceptions have a unique code, clear message, and metadata. It supports error message translation, promotes specific exception classes, and adapts details based on the environment.
Installation
Laravception requires PHP 8.2. To get the latest version, simply require the project using Composer:
Now. Publish the configuration file.
Usage
Laravception captures exceptions thrown by the application and standardizes their structure. The unified structure is as follows:
This structure will hide certain details based on whether the environment is set to development or production.
Translations
To enable exception translation, implement the interface TranslatableException. This is sufficient to start translating child exceptions.
It’s recommended to create a custom exception class for each error to allow more specific translations. For example, with the exception “InvalidCreditCardException”, you can have a translation entry in the exceptions file:exceptions:
This functionality is enabled by default. Any exceptions that do not implement Codeable will be treated in the same manner, converting the class name to snake_case and using this transformation as the error code and key for the translation.
For better error code management, you can implement Codeable. In the exception "InvalidCreditCardException", you can return a custom error code such as payments.invalid_credit_card, and in your translation file:
You can also add parameters by implementing the Parametrizable interface. Return an array with the properties to be used, and your translation file will look like this:
We can finally have this implementation:
Configuration
To understand the various configuration options, please refer to the comments in the configuration file laravception.php.
Principles
The core idea behind this library is to standardize error messages in a Laravel project, ensuring that all exceptions follow a common structure.
Specific Exceptions
To improve API organization and documentation, it is recommended to create custom exceptions with a specific purpose, such as "PaymentFailedException" or "ClientNotFoundException" This allows for more precise and effective handling of exceptions.
Translation of Messages
With specific exceptions, presenting error messages can be automated using unique codes. This makes it easier to translate and decorate error messages, ensuring they are clear and useful for both developers and end-users.
License
This library is licensed under the MIT License. For more information, please see the license
file.
All versions of laravception with dependencies
laravel/framework Version ^10.21|^v11.15
vaened/support Version ^4.0
lambdish/phunctional Version ^2.1