[6.x] Hydrate cascade for error views rendered outside Statamic's exception stack - #15201
Open
wakqasahmed wants to merge 1 commit into
Open
Conversation
… stack (statamic#14167) Statamic's own HTTP exception classes (NotFoundHttpException, ForbiddenHttpException, UnauthorizedHttpException) hydrate the cascade via RendersHttpExceptions::contents() before rendering an Antlers error template. A generic exception thrown outside that stack (e.g. Livewire's default 404 handling, which throws Symfony's stock NotFoundHttpException) never goes through that path, so referencing a global in the error template throws a BadMethodCallException. Register a renderable callback on the app's exception handler that hydrates the cascade and renders the error template through Statamic's View class whenever a generic HttpExceptionInterface exception has a matching errors.{status} view, regardless of which middleware stack or exception class triggered it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #14167
Problem
Statamic's own HTTP exception classes (
Statamic\Exceptions\NotFoundHttpException,ForbiddenHttpException,UnauthorizedHttpException) hydrate the cascade viaRendersHttpExceptions::contents()before rendering anerrors.{status}Antlers template. That's the only place that callsCascade::instance()->hydrate().A 404 (or other HTTP error) thrown from outside Statamic's own stack — e.g. Livewire's default 404 handling, which throws Symfony's stock
NotFoundHttpExceptionrather than Statamic's subclass — never goes through that path. Laravel falls back to its default exception rendering, which renders the Antlers error template directly with no cascade hydrated. Any{{ global_set_variable }}reference in that template then throws aBadMethodCallException.The CP route group already gets its own exception handler swapped in via
SwapCpExceptionHandlermiddleware, but the front-end route group has no equivalent, and that middleware only covers Statamic's own exception classes anyway — a generic Symfony exception bypasses it either way since it doesn't define its ownrender()method.Fix
Registers a
renderable()callback on the app's exception handler (src/Providers/AppServiceProvider.php) that:errors.{status}view exists.Viewclass, the same wayRendersHttpExceptions::contents()does.This only fires for exceptions that don't already define their own
render()method (Statamic's own exception classes are checked first by Laravel and never reach this callback), and it's discarded for CP routes sinceSwapCpExceptionHandlerrebinds the exception handler singleton toControlPanelExceptionHandler, which has its ownrender()that doesn't consultrenderable()callbacks.Test plan
it_hydrates_the_cascade_for_a_404_thrown_outside_of_statamics_own_exception_stacktotests/FrontendTest.php, which throws a bareSymfony\Component\HttpKernel\Exception\NotFoundHttpExceptionfrom an ad-hoc route (mirroring Livewire's default 404 path) with anerrors.404template referencing a global set variable via colon syntax, and asserts it renders successfully.tests/FrontendTest.php(FakesViews,withFakeViews/viewShouldReturnRaw) andtests/View/CascadeTest.php(GlobalSetsetup). Relying on CI to validate.