amontreuil/exception-handler-bundle
Pros:
400, 401, 404, 503) and custom business exceptions (e.g., DuplicateDataException, BadUuidException).ViolationRuleRequestException for business rule violations).Cons:
Illuminate\Foundation\Exceptions\Handler) differs from Symfony’s ProblemDetails or ExceptionListener. Direct integration may require abstraction layers.452, 456) may confuse clients or violate API design principles (e.g., RFC 7807 for ProblemDetails).symfony/http-foundation for HTTP responses, symfony/event-dispatcher for exception listeners).throw_if/render pipeline or using a facade to bridge the two.App\Exceptions\BadRequestException).ProblemDetails format may not align with Laravel’s JSON responses).App\Exceptions\Handler expects specific methods (e.g., render()), while Symfony’s ExceptionListener uses events. Mismatched patterns may require significant refactoring.ProblemDetails format, or is this a temporary solution?Problem class (via illuminate/support) or packages like fruitcake/laravel-cors achieve similar goals?452) be documented for clients, or will they be internal-only?render() logic (e.g., API responses, logging)?spatie/laravel-exception-handler) that better fit Laravel’s ecosystem?symfony/http-foundation, symfony/event-dispatcher, etc., as standalone dependencies.Problem class (for RFC 7807 compliance) or extend Illuminate\Foundation\Exceptions\Handler for custom logic.spatie/laravel-exception-handler for structured error responses.nWidart/laravel-routes-generator for route-related exceptions.App\Exceptions\BadRequestException extends \Exception) and reuse logic.452 BadFormPostException).composer require symfony/http-foundation symfony/event-dispatcher
// app/Providers/SymfonyExceptionProvider.php
use Amontreuil\ExceptionHandlerBundle\Exception\BadRequestException;
use Symfony\Component\EventDispatcher\EventDispatcher;
class SymfonyExceptionProvider extends ServiceProvider {
public function register() {
$this->app->singleton(EventDispatcher::class, fn() => new EventDispatcher());
// Register exception listeners...
}
}
app/Exceptions and extend Laravel’s Handler.// app/Exceptions/BadRequestException.php
namespace App\Exceptions;
use Illuminate\Http\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
class BadRequestException extends HttpException {
public function __construct(string $message = "The required data is missing or incorrect.")
{
parent::__construct(400, $message);
}
}
throw new \Exception() with bundle exceptions (e.g., throw new BadRequestException()).App\Exceptions\Handler to render bundle exceptions consistently:
public function render($request, Throwable $exception) {
if ($exception instanceof \Amontreuil\ExceptionHandlerBundle\Exception\ApiException) {
return response()->json([
'error' => $exception->getMessage(),
'status' => $exception->getStatusCode(),
], $exception->getStatusCode());
}
return parent::render($request, $exception);
}
BadUuidException).ExceptionListener may conflict with Laravel’s Exception middleware. Use a priority-based approach or disable Laravel’s default handler for bundle exceptions.ProblemDetails may not match Laravel’s JSON responses. Normalize output in the Handler.Fractal or Spatie packages).Monolog (Symfony’s ErrorHandler may override this).400, 401, 404) to validate integration.DuplicateDataException) after confirming the stack works.How can I help you explore Laravel packages today?