m6web/api-exception-bundle
Symfony bundle that centralizes API exception handling and returns clean JSON responses. Supports configurable error formatting, stack traces, form validation errors, exception matching/mapping, logging, and environment-specific behavior.
App\Exceptions\Handler (replaces Symfony’s EventListener).Illuminate\Validation (replaces Symfony’s Validator).DependencyInjection).APP_DEBUG mode.FormRequest validation.Nova, Sanctum, or Horizon integrations.| Symfony Component | Laravel Equivalent | Adaptation Complexity |
|---|---|---|
HttpKernel |
Illuminate\Foundation\Http |
Low |
EventDispatcher |
Illuminate\Events |
Medium |
Validator |
Illuminate\Validation\Validator |
Low |
Twig |
Blade (irrelevant for APIs) |
N/A |
DependencyInjection |
Illuminate\Container |
Medium |
| Risk Factor | Impact Assessment | Mitigation Strategy |
|---|---|---|
| Symfony2 Deprecation | Bundle relies on unsupported Symfony2 components (e.g., HttpKernel). |
Abstract dependencies into Laravel-compatible interfaces or use Symfony5+ bridges. |
| Lack of Maintenance | Last release in 2019; no Laravel-specific updates. | Fork the repo and maintain a Laravel branch, or rewrite only the needed logic. |
| Configuration Drift | Symfony’s YAML/XML config → Laravel’s PHP arrays. | Convert config to config/exceptions.php with Laravel’s config() helper. |
| Testing Gaps | No Laravel test suite; potential edge cases in validation/error handling. | Write Pest/PHPUnit tests for adapted exception classes. |
| Performance Overhead | Symfony’s event system may add latency if not optimized. | Use Laravel middleware (lighter than Symfony events) for exception handling. |
| Validation Integration | Symfony’s Validator → Laravel’s Validator may have API differences. |
Test validation error payloads against Laravel’s FormRequest validation. |
App\Exceptions\Handler for basic JSON responses.spatie/laravel-fractal (API response formatting).fruitcake/laravel-cors (CORS + error handling).App\Exceptions\Handler with response()->json().Laravel Compatibility Matrix:
| Feature | Laravel Equivalent | Integration Path |
|---|---|---|
| Exception Handling | App\Exceptions\Handler |
Override render() to return JSON responses. |
| Validation Errors | Illuminate\Validation\Validator |
Extend FormRequest or use Validator::make() in exception logic. |
| Event-Driven Logic | Illuminate\Events |
Replace Symfony’s EventDispatcher with Laravel’s event() helper. |
| HTTP Responses | Illuminate\Http\JsonResponse |
Use response()->json() instead of Symfony’s JsonResponse. |
| Configuration | config/exceptions.php |
Convert YAML/XML to Laravel’s PHP array format. |
| Dependency Injection | Illuminate\Container |
Bind exceptions/services in AppServiceProvider. |
| Logging | Illuminate\Log |
Use Laravel’s Log facade instead of Symfony’s LoggerInterface. |
Recommended Stack:
Step 1: Define Laravel-Compatible Exception Classes (1 day)
app/Exceptions/ApiException.php:
namespace App\Exceptions;
use Exception;
use Illuminate\Http\JsonResponse;
class ApiException extends Exception {
public function toJson(): JsonResponse {
return response()->json([
'error' => [
'code' => $this->getCode(),
'message' => $this->getMessage(),
'status' => $this->httpStatusCode ?? 500,
'details' => $this->details ?? null,
],
], $this->httpStatusCode);
}
}
app/Exceptions/ValidationException.php.Step 2: Override App\Exceptions\Handler (1 day)
render() to catch exceptions and return JSON:
public function render($request, Throwable $exception) {
if ($exception instanceof \App\Exceptions\ApiException) {
return $exception->toJson();
}
return parent::render($request, $exception);
}
Step 3: Add Configuration (0.5 days)
config/exceptions.php:
return [
'codes' => [
'validation' => 422,
'not_found' => 404,
],
'debug' => [
'show_stack_trace' => env('APP_DEBUG'),
],
];
Step 4: Test Edge Cases (1 day)
throw new ApiException("Test", 400)).$validator->errors()->toJson()).APP_DEBUG).laravel-api-exception-bundle.symfony/http-foundation bridge for JsonResponse.EventDispatcher with Laravel’s Events.How can I help you explore Laravel packages today?