Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions src/Exceptions/AdminErrorPages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
15 changes: 12 additions & 3 deletions tests/Feature/AdminErrorPagesTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Illuminate\Foundation\Auth\User;
use Illuminate\Http\Request;
use Step2dev\LazyAdmin\Exceptions\AdminErrorPages;
use Symfony\Component\HttpKernel\Exception\HttpException;
Expand Down Expand Up @@ -42,16 +43,16 @@
it('avoids the admin shell for guest requests and recoverable session errors', function (): void {
$renderer = new AdminErrorPages;

foreach ([401, 403, 404, 419, 429, 500, 503] as $status) {
foreach ([401, 403, 419, 429, 500, 503] as $status) {
$response = $renderer(new HttpException($status), Request::create('/admin/missing'));

expect($response?->getStatusCode())->toBe($status)
->and($response?->getContent())->not->toContain('data-lazy-admin-shell');
}
});

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);

Expand All @@ -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');
});
Loading