From 1cf1ab79d5e2401100dc48ecc19b1a5b946d0d1c Mon Sep 17 00:00:00 2001 From: Julian Prieber Date: Fri, 3 Jan 2025 14:19:01 +0100 Subject: [PATCH] Enhance exception handling in the Handler class and improve error display in dashboard-error view --- app/Exceptions/Handler.php | 42 ++++++++++--------- .../views/errors/dashboard-error.blade.php | 11 +++-- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index deda659..6e56973 100755 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -42,28 +42,32 @@ class Handler extends ExceptionHandler $this->renderable(function (Throwable $e, Request $request) { - try { - // Check if the request matches a specific route - if ($request->is('dashboard') || - $request->is('dashboard/*') || - $request->is('admin/*') || - $request->is('studio/*')) { - - // Handle 404 errors for this specific route - if ($e instanceof NotFoundHttpException) { - $message = "The page you are looking for was not found."; - return response()->view('errors.dashboard-error', compact('message'), 404); - } + if ($e->getPrevious() instanceof \Illuminate\Session\TokenMismatchException) { - // Handle general exceptions for this specific route - - $error = $e; - $message = $e->getMessage(); - return response()->view('errors.dashboard-error', compact(['error', 'message']), 500); + return redirect()->route('login')->withErrors(['email' => 'Your session has expired. Please log in again.']); + } - // Default exception handling for other routes - return parent::render($request, $e); + try { + $patterns = ['dashboard', 'dashboard/*', 'admin/*', 'studio/*']; + + if (collect($patterns)->contains(fn($pattern) => $request->is($pattern))) { + + if ($e instanceof NotFoundHttpException) { + + $message = "The page you are looking for was not found."; + + return response()->view('errors.dashboard-error', compact('message'), 404); + + } + + $error = $e; + $message = $e->getMessage(); + + return response()->view('errors.dashboard-error', compact(['error', 'message']), 500); + + } + } catch (Throwable $e) {} }); diff --git a/resources/views/errors/dashboard-error.blade.php b/resources/views/errors/dashboard-error.blade.php index 46b7cc7..4b8c11a 100644 --- a/resources/views/errors/dashboard-error.blade.php +++ b/resources/views/errors/dashboard-error.blade.php @@ -35,9 +35,14 @@