Installation
composer require bensonirah/handler-exception
Register the service provider in config/app.php:
'providers' => [
// ...
Bensonirah\HandlerException\HandlerExceptionServiceProvider::class,
],
Basic Configuration Publish the config file:
php artisan vendor:publish --provider="Bensonirah\HandlerException\HandlerExceptionServiceProvider" --tag="config"
Edit config/handler-exception.php to define your exception handlers (e.g., App\Exceptions\Handler or custom classes).
First Use Case
Define a handler for a specific exception in config/handler-exception.php:
'handlers' => [
\App\Exceptions\CustomException::class => \App\Http\Controllers\ExceptionController::class,
],
Trigger an exception in your controller:
throw new \App\Exceptions\CustomException('Test error');
The package will automatically route it to ExceptionController.
ExceptionListener and ExceptionHandler interfaces for structured handling.
use Bensonirah\HandlerException\Contracts\ExceptionHandlerInterface;
class CustomExceptionHandler implements ExceptionHandlerInterface
{
public function handle(\Throwable $exception, $code)
{
return response()->json(['error' => $exception->getMessage()], $code);
}
}
Register in config/handler-exception.php:
'handlers' => [
\App\Exceptions\CustomException::class => CustomExceptionHandler::class,
],
// app/Http/Middleware/AssignExceptionHandler.php
public function handle($request, Closure $next)
{
if ($request->is('api/*')) {
config(['handler-exception.handlers' => [
\App\Exceptions\ApiException::class => \App\Http\Controllers\ApiExceptionController::class,
]]);
}
return $next($request);
}
'handlers' => [
[
\App\Exceptions\ValidationException::class,
\App\Exceptions\AuthException::class,
] => \App\Http\Controllers\GlobalExceptionController::class,
],
use Illuminate\Support\Facades\Log;
class LoggingExceptionHandler implements ExceptionHandlerInterface
{
public function handle(\Throwable $exception, $code)
{
Log::error($exception->getMessage(), ['exception' => $exception]);
return response()->json(['error' => 'Internal Server Error'], $code);
}
}
Config Overrides
App\Exceptions\Handler if using this package, as it may conflict with the centralized config.Middleware Order
HandlerExceptionMiddleware runs after your app’s middleware (e.g., auth, verified) to avoid premature exception handling.$middlewareGroups['web'] or $middlewareGroups['api'].Circular Dependencies
if ($exception instanceof \Bensonirah\HandlerException\Exceptions\HandlerException) {
return response()->json(['error' => 'Handler error'], 500);
}
Verify Handler Registration
config/handler-exception.php. Use:
php artisan config:clear
to reset cached config.Log Unhandled Exceptions
APP_DEBUG=true) to log unhandled exceptions to storage/logs/laravel.log.Test Edge Cases
try-catch throwing a new exception).RuntimeException).Custom Exception Classes
Bensonirah\HandlerException\Exceptions\HandlerException for domain-specific exceptions:
class PaymentException extends HandlerException {}
Event Listeners
exception.handled events to perform post-handling actions:
Event::listen('exception.handled', function ($exception, $handler) {
// Send notification, update analytics, etc.
});
API Responses
class ApiExceptionHandler implements ExceptionHandlerInterface
{
public function handle(\Throwable $exception, $code)
{
return response()->json([
'success' => false,
'error' => $exception->getMessage(),
'code' => $code,
], $code);
}
}
How can I help you explore Laravel packages today?