diff --git a/bootstrap/app.php b/bootstrap/app.php index f00bc76b..fa4f430d 100755 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -5,6 +5,17 @@ use Illuminate\Foundation\Application; use Illuminate\Foundation\Configuration\Exceptions; use Illuminate\Foundation\Configuration\Middleware; +use Illuminate\Http\Request; +use Inertia\Inertia; +use Symfony\Component\HttpFoundation\Response; + +const HTTP_SESSION_EXPIRED = 419; +const HANDLED_ERROR_CODES = [ + Response::HTTP_FORBIDDEN, + Response::HTTP_INTERNAL_SERVER_ERROR, + Response::HTTP_SERVICE_UNAVAILABLE, + Response::HTTP_NOT_FOUND, +]; return Application::configure(basePath: dirname(__DIR__)) ->withRouting( @@ -16,4 +27,17 @@ ->withMiddleware(function (Middleware $middleware): void { }) ->withExceptions(function (Exceptions $exceptions): void { + $exceptions->respond(function (Response $response, Throwable $exception, Request $request): Response { + if (!app()->environment(["local", "testing"]) && in_array($response->getStatusCode(), HANDLED_ERROR_CODES, true)) { + return Inertia::render("Errors/Error", ["status" => $response->getStatusCode()]) + ->toResponse($request) + ->setStatusCode($response->getStatusCode()); + } elseif ($response->getStatusCode() === HTTP_SESSION_EXPIRED) { + return back()->with([ + "message" => "The page expired, please try again.", + ]); + } + + return $response; + }); })->create(); diff --git a/resources/js/Pages/Errors/Error.vue b/resources/js/Pages/Errors/Error.vue new file mode 100644 index 00000000..96c7571b --- /dev/null +++ b/resources/js/Pages/Errors/Error.vue @@ -0,0 +1,43 @@ + + + + +
+ + +