Skip to content

Deduplicate template error handling - #4882

Merged
fabpot merged 1 commit into
twigphp:3.xfrom
fabpot:template-error-handler
Aug 3, 2026
Merged

Deduplicate template error handling#4882
fabpot merged 1 commit into
twigphp:3.xfrom
fabpot:template-error-handler

Conversation

@fabpot

@fabpot fabpot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@upsun-dispatch

upsun-dispatch Bot commented Aug 3, 2026

Copy link
Copy Markdown

📋 PR Summary

This PR deduplicates the exception-handling logic in Template::yield() and Template::yieldBlock() by extracting the two separate catch blocks into a single private handleException() helper. The single catch (\Throwable $e) now delegates to that helper, which enriches Twig Error instances (source context + line guessing) or wraps non-Twig throwables in a RuntimeError. A test confirms that a thrown Twig RuntimeError is enriched in place rather than re-wrapped.

Changes
Layer / File(s) Summary
error handling refactor
src/Template.php Replaced the duplicated catch (Error) / catch (\Throwable) blocks in yield() and yieldBlock() with a single catch that calls the new handleException() helper, which preserves the original enrich-vs-wrap behavior via an instanceof Error check.
tests
tests/ErrorTest.php Added a test verifying a Twig RuntimeError thrown from a function is enriched (source context and line set) and re-thrown as the same instance rather than wrapped.

@fabpot
fabpot force-pushed the template-error-handler branch from c0cbce1 to e62abbf Compare August 3, 2026 15:28
@fabpot
fabpot merged commit e1bc281 into twigphp:3.x Aug 3, 2026
54 checks passed
@fabpot
fabpot deleted the template-error-handler branch August 3, 2026 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants