|
1 | 1 | <?php
|
2 | 2 |
|
| 3 | +use App\Exceptions\ErrorToastException; |
3 | 4 | use App\Http\Middleware\EncryptCookies;
|
4 | 5 | use App\Http\Middleware\HandleInertiaRequests;
|
5 | 6 | use Illuminate\Cookie\Middleware\EncryptCookies as BaseEncryptCookies;
|
|
10 | 11 | use Illuminate\Http\Request;
|
11 | 12 | use Inertia\Inertia;
|
12 | 13 | use Symfony\Component\HttpFoundation\Response;
|
| 14 | +use Tighten\Ziggy\Ziggy; |
13 | 15 |
|
14 | 16 | return Application::configure(basePath: dirname(__DIR__))
|
15 | 17 | ->withRouting(
|
|
30 | 32 | })
|
31 | 33 | ->withExceptions(function (Exceptions $exceptions) {
|
32 | 34 | $exceptions->respond(function (Response $response, Throwable $exception, Request $request) {
|
33 |
| - if ( |
34 |
| - !app()->environment(['local', 'testing']) |
35 |
| - && in_array($response->getStatusCode(), [500, 503, 404, 403]) |
36 |
| - ) { |
37 |
| - return Inertia::render('Error', [ |
38 |
| - 'homepageRoute' => route('welcome'), |
39 |
| - 'status' => $response->getStatusCode() |
40 |
| - ]) |
41 |
| - ->toResponse($request) |
42 |
| - ->setStatusCode($response->getStatusCode()); |
43 |
| - } elseif ($response->getStatusCode() === 419) { |
| 35 | + $statusCode = $response->getStatusCode(); |
| 36 | + $errorTitles = [ |
| 37 | + 403 => 'Forbidden', |
| 38 | + 404 => 'Not Found', |
| 39 | + 500 => 'Server Error', |
| 40 | + 503 => 'Service Unavailable', |
| 41 | + ]; |
| 42 | + $errorDetails = [ |
| 43 | + 403 => 'Sorry, you are unauthorized to access this resource/action.', |
| 44 | + 404 => 'Sorry, the resource you are looking for could not be found.', |
| 45 | + 500 => 'Whoops, something went wrong on our end. Please try again.', |
| 46 | + 503 => 'Sorry, we are doing some maintenance. Please check back soon.', |
| 47 | + ]; |
| 48 | + |
| 49 | + if (in_array($statusCode, [500, 503, 404, 403])) { |
| 50 | + if (!$request->inertia()) { |
| 51 | + // Show error page component for standard visits |
| 52 | + return Inertia::render('Error', [ |
| 53 | + 'errorTitles' => $errorTitles, |
| 54 | + 'errorDetails' => $errorDetails, |
| 55 | + 'status' => $statusCode, |
| 56 | + 'homepageRoute' => route('welcome'), |
| 57 | + 'ziggy' => fn () => [ |
| 58 | + ...(new Ziggy())->toArray(), |
| 59 | + 'location' => $request->url(), |
| 60 | + ], |
| 61 | + ]) |
| 62 | + ->toResponse($request) |
| 63 | + ->setStatusCode($statusCode); |
| 64 | + } else { |
| 65 | + // Show standard modal for easier debugging locally |
| 66 | + if (app()->isLocal() && $statusCode === 500) { |
| 67 | + return $response; |
| 68 | + } |
| 69 | + // Return JSON response for PrimeVue toast to display, handled by Inertia router event listener |
| 70 | + $errorSummary = "$statusCode - $errorTitles[$statusCode]"; |
| 71 | + $errorDetail = $errorDetails[$statusCode]; |
| 72 | + if (get_class($exception) === ErrorToastException::class) { |
| 73 | + $errorSummary = "$statusCode - Error"; |
| 74 | + $errorDetail = $exception->getMessage(); |
| 75 | + } |
| 76 | + return response()->json([ |
| 77 | + 'error_summary' => $errorSummary, |
| 78 | + 'error_detail' => $errorDetail, |
| 79 | + ], $statusCode); |
| 80 | + } |
| 81 | + } elseif ($statusCode === 419) { |
44 | 82 | return back()->with([
|
45 | 83 | 'flash_warn' => 'The page expired, please try again.',
|
46 | 84 | ]);
|
|
0 commit comments