bytestechnolabs/custom-exception-handler
Laravel package for configurable exception handling: map exception classes to custom messages, HTTP status codes, and extra response data via config. Includes helper to add custom data dynamically during catch blocks for consistent API error responses.
Certainly, here's a sample README file for your Laravel package, the Custom Exception Handler:
# Custom Exception Handler for Laravel
The **Custom Exception Handler for Laravel** is a package designed to provide custom exception handling within your Laravel application. It allows you to specify how different types of exceptions should be handled, set custom HTTP response codes, define error messages, and even provide custom data to be included in the response.
## Installation
You can easily install the package via Composer:
```shell
composer require bytestechnolabs/custom-exception-handler
After installation, you can configure the Custom Exception Handler by publishing the configuration file. This file enables you to define which exceptions to handle and how they should be handled.
To publish the configuration file, run the following command:
php artisan vendor:publish --provider="BytesTechnolabs\CustomExceptionHandler\CustomExceptionHandlerServiceProvider"
The Custom Exception Handler allows you to handle specific exceptions differently. You can customize:
Here is an example of how to handle a CustomException:
'exceptions' => [
// ... Other exception classes
'CustomException',
],
CustomException:'exceptions_messages' => [
// ... Other exception messages
'CustomException' => 'A custom exception occurred.',
],
'exceptions_status_codes' => [
// ... Other exception status codes
'CustomException' => 400,
],
'custom_data' => [
'CustomException' => [
'custom_key' => 'custom_data_key',
'data' => ['custom' => 'data'],
],
],
You can also add custom data dynamically during exception handling by using the CustomDataHelper class provided by the package:
use BytesTechnolabs\CustomExceptionHandler\CustomDataHelper;
try {
// Code that may throw an exception
} catch (\Exception $exception) {
// Add custom data to the response
CustomDataHelper::addCustomDataToConfig($exception, ['custom' => 'data'], 'custom_data_key');
// Rethrow the Exception
throw $exception;
}
If you encounter any issues or have questions about the Custom Exception Handler for Laravel, please report them on GitHub.
This package is open-source software licensed under the MIT License.
The current version of the package is 1.6.0.
For release notes and changes, please refer to the GitHub releases.
We hope you find the Custom Exception Handler for Laravel useful. If you have any suggestions or improvements for this package, feel free to contribute to the project on GitHub.
You can copy and paste this README into your project's README.md file and make any necessary adjustments to reflect your package's specifics.
How can I help you explore Laravel packages today?