ldc-arcadia-api/bootstrap/exceptions.php
Jérémy Munsch a93fde3a3c
feat(api): implement rate limiting and error handling
- Added rate limiting for web and API requests.
- Introduced custom error handling for "Too Many Requests" response.
- Created view for "Too Many Requests" error documentation.
2026-08-12 17:25:45 +02:00

35 lines
1 KiB
PHP

<?php
declare(strict_types=1);
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Http\Request;
use Presentation\Api\Exceptions\ApiExceptionDispatcher;
use Presentation\Web\Exceptions\WebExceptionRenderer;
use Sentry\State\HubInterface;
return function (Exceptions $exceptions): void {
$exceptions->reportable(function (\Throwable $e): void {
if (app()->bound(HubInterface::class)) {
resolve(HubInterface::class)->captureException($e);
}
});
$exceptions->render(function (\Throwable $e, Request $request) {
$apiDispatcher = resolve(ApiExceptionDispatcher::class);
if ($apiResponse = $apiDispatcher->render($e, $request)) {
return $apiResponse;
}
$webRenderer = resolve(WebExceptionRenderer::class);
if ($webResponse = $webRenderer->render($e, $request)) {
return $webResponse;
}
return null;
});
$exceptions->shouldRenderJsonWhen(
fn (Request $request) => $request->is('api/*'),
);
};