Skip to content

Commit e47f44a

Browse files
Catch all errors thrown when generating shortcode replacements (#17)
When a controller that is used to generate shortcode replacements throws an Exception, this exeception will currently fall-through. I think this kind of errors should be contained, and we should try to display the shortcode as unchanged as possible, as if we had not attempted to resolve it in the first place (of course, logging the problem). Co-authored-by: Fabian Schmick <[email protected]>
1 parent e737d32 commit e47f44a

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/Handler/EmbeddedShortcodeHandler.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Symfony\Component\HttpFoundation\RequestStack;
88
use Symfony\Component\HttpKernel\Controller\ControllerReference;
99
use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
10+
use Throwable;
11+
use Thunder\Shortcode\Shortcode\ProcessedShortcode;
1012
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
1113

1214
/**
@@ -66,9 +68,22 @@ public function __invoke(ShortcodeInterface $shortcode)
6668
]
6769
);
6870

69-
return $this->fragmentHandler->render(
70-
new ControllerReference($this->controllerName, $shortcode->getParameters()),
71-
$this->renderer
72-
);
71+
try {
72+
return $this->fragmentHandler->render(
73+
new ControllerReference($this->controllerName, $shortcode->getParameters()),
74+
$this->renderer
75+
);
76+
} catch (Throwable $exception) {
77+
$this->logger->error('An exception was thrown when rendering the shortcode', ['exception' => $exception]);
78+
79+
if ($shortcode instanceof ProcessedShortcode) {
80+
$text = trim($shortcode->getShortcodeText(), '[]');
81+
} else {
82+
$text = $shortcode->getName().' ...';
83+
}
84+
85+
// Avoid an infinite loop that occurs if the original shortcode is returned
86+
return "<code>&#91;$text&#93;</code>";
87+
}
7388
}
7489
}

0 commit comments

Comments
 (0)