Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot override default error page with Laravel 11 Bootstrap Exception Handling #52469

Closed
rndb8418 opened this issue Aug 13, 2024 · 2 comments
Closed

Comments

@rndb8418
Copy link
Contributor

Laravel Version

11.20.0

PHP Version

8.3.1

Database Driver & Version

No response

Description

In Laravel 10, it was possible to utilise Laravel's exception handling for internal/application errors, whilst having a fallback for errors that did not meet that criteria (e.g. incorrect database credentials).

For example, in Laravel 10, the App\Exceptions\Handler@render method could be...

public function render($request, Throwable $exception)
{
    // If local environment, or an exception that should not be reported,
    // allow Laravel to handle rendering.
    if (app()->environment('local') || $this->shouldntReport($exception)) {
        return parent::render($request, $exception);
    }

    // Specific handling of TokenMismatchException (e.g., redirect with error message, or return JSON error if AJAX).
    // This CAN be handled in Laravel 11
    if ($exception instanceof TokenMismatchException) {
        return $this->handleTokenMismatch($request);
    }

    // Get an appropriate status code for the exception, defaulting to 500.
    $code = $this->getStatusCode($exception);

    // Return a generic error page to the user
    return response()->view('errors.generic', ['code' => $code], $code);
}

In Laravel 11, this does not appear to be possible without overriding the whole exception handler due to the loss of access to knowing if an exception should be ignored or not. Something like this does not work as it would return the generic error pages for errors like unauthenticated etc.

$exceptions->render(function (Throwable $e, Request $request) use ($exceptions) {
    // Not possible to check if an exception is unreportable
    if (app()->environment('local')) {
        return;
    }

    // Get the status code from the exception
    $code = ...;

    return response()->view('errors.generic', ['code' => $code], $code);
});

If no rendering is specified and it's left to Laravel, then errors that do not return specific HTTP exceptions (the easiest to replicate being incorrect database credentials) will return a generic Symfony error page. These exceptions ignore the 5xx, 500 etc. custom error pages that you can configure.

Steps To Reproduce

  1. Generate an exception that does not have HTTP error handling, such as incorrect database credentials in the .env
  2. Load page that triggers the exception
  3. You'll see Symfony's generic error page shown

image

@crynobone
Copy link
Member

Hey there, thanks for reporting this issue.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here?

Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

Do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

@rndb8418
Copy link
Contributor Author

After attempting to create a replicate (and being unable to), I narrowed it down to an exception occuring within our error template under certain conditions (e.g. no database/invalid credentials). This was causing by a view composer that was applied to the top-most layout, that all other layouts extended.

When Laravel attempted to render our error pages, it would encounter an exception and result in Laravel returning a Symfony error page.

I've fixed it by removing the specific logic from the top-most layout, alternatively I could have used a simplified error layout.

Apologies for the false report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants