diff --git a/src/Exceptions/AdminErrorPages.php b/src/Exceptions/AdminErrorPages.php index b0f9f4c..e362173 100644 --- a/src/Exceptions/AdminErrorPages.php +++ b/src/Exceptions/AdminErrorPages.php @@ -3,8 +3,8 @@ namespace Step2dev\LazyAdmin\Exceptions; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Auth; use Illuminate\Http\Response; +use Illuminate\Support\Facades\Auth; use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; use Throwable; @@ -27,13 +27,28 @@ public function __invoke(Throwable $exception, Request $request): ?Response } $guard = (string) config('lazy.auth.guard', 'web'); - $useAdminLayout = in_array($status, [403, 404], true) && Auth::guard($guard)->check(); - - return response()->view('lazy::errors.page', [ - 'status' => $status, - 'useAdminLayout' => $useAdminLayout, - 'homeUrl' => url('/'.trim((string) config('lazy.admin.route.prefix', 'admin'), '/')), - ], $status, $exception->getHeaders()); + $useAdminLayout = $status === 404 || ($status === 403 && Auth::guard($guard)->check()); + + try { + return response()->view('lazy::errors.page', [ + 'status' => $status, + 'useAdminLayout' => $useAdminLayout, + 'homeUrl' => url('/'.trim((string) config('lazy.admin.route.prefix', 'admin'), '/')), + ], $status, $exception->getHeaders()); + } catch (Throwable $renderFailure) { + if (! $useAdminLayout) { + throw $renderFailure; + } + + // A missing admin page must never escalate into a secondary server error. + report($renderFailure); + + return response()->view('lazy::errors.page', [ + 'status' => $status, + 'useAdminLayout' => false, + 'homeUrl' => url('/'.trim((string) config('lazy.admin.route.prefix', 'admin'), '/')), + ], $status, $exception->getHeaders()); + } } private function isAdminRequest(Request $request): bool diff --git a/tests/Feature/AdminErrorPagesTest.php b/tests/Feature/AdminErrorPagesTest.php index b6ba724..e20a8ce 100644 --- a/tests/Feature/AdminErrorPagesTest.php +++ b/tests/Feature/AdminErrorPagesTest.php @@ -1,5 +1,6 @@ getStatusCode())->toBe($status) @@ -50,8 +51,8 @@ } }); -it('uses the actual admin layout for signed-in missing pages', function (): void { - $user = new class extends \\Illuminate\\Foundation\\Auth\\User {}; +it('uses the actual admin layout for missing pages regardless of the authentication flag', function (): void { + $user = new class extends User {}; $user->id = 123; $this->be($user); @@ -61,3 +62,11 @@ ->and($response?->getContent())->toContain('data-lazy-admin-shell') ->and($response?->getContent())->toContain('Back to dashboard'); }); + +it('attempts full admin rendering for a guest 404 instead of the standalone template', function (): void { + $renderer = new AdminErrorPages; + $response = $renderer(new HttpException(404), Request::create('/admin/missing')); + + expect($response?->getStatusCode())->toBe(404) + ->and($response?->getContent())->toContain('data-lazy-admin-shell'); +});