Skip to content

Commit db0b769

Browse files
committed
feature #1132 Avoiding double-wrapping of Twig RuntimeError (weaverryan)
This PR was merged into the 2.x branch. Discussion ---------- Avoiding double-wrapping of Twig RuntimeError | Q | A | ------------- | --- | Bug fix? | yes | New feature? | yes | Tickets | None | License | MIT I had noticed that, when making a typo in my Twig file, the true error was hidden inside a nested exception. **Before (left top of page, right, further down the page)** <img width="400" alt="Screenshot 2023-09-22 at 8 21 05 AM" src="https://github.com/symfony/ux/assets/121003/5f17b58e-a9fe-40e9-a85b-90aadec5b1cd"><img width="400" alt="Screenshot 2023-09-22 at 8 21 16 AM" src="https://github.com/symfony/ux/assets/121003/e5d38f99-adca-44a6-b845-276348623da4"> **After** <img width="400" alt="Screenshot 2023-09-22 at 8 18 24 AM" src="https://github.com/symfony/ux/assets/121003/ad18117f-1b78-4fdc-80e3-d1550859ebf6"> So, you see the exact error now. You lose that the error happened when rendering the `InvoiceCreator` component, but I think that's not important. And you can find that actually if you look up the stacktrace a bit. Btw, the reason we want to throw a `RuntimeError` from Twig is if, when rendering, if you have an error in your PHP code (not template), it makes it look nicer. Without wrapping your PHP error in a `RuntimeError`, you get: > An exception has been thrown during the rendering of a template ("AHhhh"). After, it is: > Error rendering "Icon" component: AHhhh So the `RuntimeError` is 👍 ... but we don't need to create a 2nd one if we already have one. Cheers! Commits ------- 8cef119 Avoiding double-wrapping of Twig RuntimeError
2 parents c3a4e7f + 8cef119 commit db0b769

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/TwigComponent/src/Twig/ComponentExtension.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,15 @@ public function finishEmbeddedComponentRender(): void
8787

8888
private function throwRuntimeError(string $name, \Throwable $e): void
8989
{
90+
// if it's already a Twig RuntimeError, just rethrow it
91+
if ($e instanceof RuntimeError) {
92+
throw $e;
93+
}
94+
9095
if (!($e instanceof \Exception)) {
9196
$e = new \Exception($e->getMessage(), $e->getCode(), $e->getPrevious());
9297
}
98+
9399
throw new RuntimeError(sprintf('Error rendering "%s" component: %s', $name, $e->getMessage()), previous: $e);
94100
}
95101
}

0 commit comments

Comments
 (0)