diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 627032f..bd70589 100755 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -3,8 +3,9 @@ namespace App\Exceptions; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; -use Throwable; +use Illuminate\Http\Request; use Inertia\Inertia; +use Throwable; class Handler extends ExceptionHandler { @@ -56,12 +57,19 @@ class Handler extends ExceptionHandler public function render($request, Throwable $e) { $response = parent::render($request, $e); + $statusCode = $response->getStatusCode(); - if (! app()->environment(['local', 'testing']) && in_array($response->getStatusCode(), [500, 503, 404, 403])) { - return Inertia::render('Error', ['status' => $response->getStatusCode()]) - ->toResponse($request) - ->setStatusCode($response->getStatusCode()); - } elseif ($response->getStatusCode() === 419) { + if (in_array($statusCode, [500, 503, 404, 403])) { + if ($this->isDashboardRequest($request)) { + return $this->renderDashboardError($request, $e, $statusCode); + } + + if (! app()->environment(['local', 'testing'])) { + return Inertia::render('Error', ['status' => $statusCode]) + ->toResponse($request) + ->setStatusCode($statusCode); + } + } elseif ($statusCode === 419) { return back()->with([ 'message' => 'The page expired, please try again.', ]); @@ -70,4 +78,59 @@ class Handler extends ExceptionHandler return $response; } + /** + * Check if the request is for a dashboard route. + */ + private function isDashboardRequest(Request $request): bool + { + return str_starts_with($request->path(), 'dashboard'); + } + + /** + * Render error page with full context for dashboard users. + */ + private function renderDashboardError(Request $request, Throwable $e, int $statusCode) + { + $user = $request->user(); + $isSuperAdmin = $user && $user->hasRole('super_admin'); + $isProduction = app()->environment('production'); + + $props = [ + 'status' => $statusCode, + 'message' => $this->getErrorMessage($e, $statusCode), + 'url' => $request->fullUrl(), + 'method' => $request->method(), + 'user' => $user ? $user->only('id', 'name', 'email') : null, + 'timestamp' => now()->toIso8601String(), + ]; + + if ($isSuperAdmin && !$isProduction) { + $props['stackTrace'] = $e->getTraceAsString(); + $props['requestParams'] = $request->except([ + 'password', + 'password_confirmation', + 'current_password', + '_token', + ]); + } + + return Inertia::render('Dashboard/DashboardError', $props) + ->toResponse($request) + ->setStatusCode($statusCode); + } + + /** + * Get a user-friendly error message based on status code. + */ + private function getErrorMessage(Throwable $e, int $statusCode): string + { + $messages = [ + 503 => 'Сервис временно недоступен. Попробуйте позже.', + 500 => 'Внутренняя ошибка сервера.', + 404 => 'Запрашиваемая страница не найдена.', + 403 => 'У вас нет доступа к этой странице.', + ]; + + return $messages[$statusCode] ?? 'Произошла неизвестная ошибка.'; + } } \ No newline at end of file diff --git a/app/Ship/Exceptions/Handler.php b/app/Ship/Exceptions/Handler.php index d9576a0..0667591 100755 --- a/app/Ship/Exceptions/Handler.php +++ b/app/Ship/Exceptions/Handler.php @@ -3,6 +3,7 @@ namespace App\Ship\Exceptions; use App\Ship\Abstracts\Exceptions\Handler as AbstractHandler; +use Illuminate\Http\Request; use Inertia\Inertia; use Throwable; @@ -52,12 +53,19 @@ class Handler extends AbstractHandler public function render($request, Throwable $e) { $response = parent::render($request, $e); + $statusCode = $response->getStatusCode(); - if (! app()->environment(['local', 'testing']) && in_array($response->getStatusCode(), [500, 503, 404, 403])) { - return Inertia::render('Error', ['status' => $response->getStatusCode()]) - ->toResponse($request) - ->setStatusCode($response->getStatusCode()); - } elseif ($response->getStatusCode() === 419) { + if (in_array($statusCode, [500, 503, 404, 403])) { + if ($this->isDashboardRequest($request)) { + return $this->renderDashboardError($request, $e, $statusCode); + } + + if (! app()->environment(['local', 'testing'])) { + return Inertia::render('Error', ['status' => $statusCode]) + ->toResponse($request) + ->setStatusCode($statusCode); + } + } elseif ($statusCode === 419) { return back()->with([ 'message' => 'The page expired, please try again.', ]); @@ -65,4 +73,60 @@ class Handler extends AbstractHandler return $response; } + + /** + * Check if the request is for a dashboard route. + */ + private function isDashboardRequest(Request $request): bool + { + return str_starts_with($request->path(), 'dashboard'); + } + + /** + * Render error page with full context for dashboard users. + */ + private function renderDashboardError(Request $request, Throwable $e, int $statusCode) + { + $user = $request->user(); + $isSuperAdmin = $user && $user->hasRole('super_admin'); + $isProduction = app()->environment('production'); + + $props = [ + 'status' => $statusCode, + 'message' => $this->getErrorMessage($e, $statusCode), + 'url' => $request->fullUrl(), + 'method' => $request->method(), + 'user' => $user ? $user->only('id', 'name', 'email') : null, + 'timestamp' => now()->toIso8601String(), + ]; + + if ($isSuperAdmin && !$isProduction) { + $props['stackTrace'] = $e->getTraceAsString(); + $props['requestParams'] = $request->except([ + 'password', + 'password_confirmation', + 'current_password', + '_token', + ]); + } + + return Inertia::render('Dashboard/DashboardError', $props) + ->toResponse($request) + ->setStatusCode($statusCode); + } + + /** + * Get a user-friendly error message based on status code. + */ + private function getErrorMessage(Throwable $e, int $statusCode): string + { + $messages = [ + 503 => 'Сервис временно недоступен. Попробуйте позже.', + 500 => 'Внутренняя ошибка сервера.', + 404 => 'Запрашиваемая страница не найдена.', + 403 => 'У вас нет доступа к этой странице.', + ]; + + return $messages[$statusCode] ?? 'Произошла неизвестная ошибка.'; + } }