Restore view factory state on caught exception #61
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.
What
Restore view factory state on caught exception.
Why
When an exception is thrown while rendering a database view, the rendering state isn't restored, leading to caching issues. This problem can be notably observed within a queue worker, where the application lifecycle doesn't terminate after an exception is caught, leading to subsequent rendering inconsistencies.
The main use case leading to this fix was the following: after failing to render a view used in a queued Mailable (view factory
$renderCount
was> 0
after the exception was handled), the next sent email using a file-based Blade template was successfully built (counter still ended at> 0
, same value as the previous render, different cache keys within the view factory), but the third email being rendered and send (also file-based) contained the previous email context and sections, when both last two emails were derived from the same template (the two templates could be different, as long as they shared the same base template using@extend
and@section
).How
When an exception is caught in the render phase, the factory state is restored (see the original View implementation.
How to test
Without this fix, using Tinker, render a DB view that is supposed to throw (e.g. omit a portion of the view-model). In the same Tinker session, render the same file-based template twice, using a different view-model. The second render should contain the previous context.
With this fix, we should no longer be able to replicate this behaviour.