Skip to content

Commit

Permalink
Catch all errors thrown when generating shortcode replacements (#17)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
mpdude and FabianSchmick authored May 17, 2022
1 parent e737d32 commit e47f44a
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/Handler/EmbeddedShortcodeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Controller\ControllerReference;
use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
use Throwable;
use Thunder\Shortcode\Shortcode\ProcessedShortcode;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;

/**
Expand Down Expand Up @@ -66,9 +68,22 @@ public function __invoke(ShortcodeInterface $shortcode)
]
);

return $this->fragmentHandler->render(
new ControllerReference($this->controllerName, $shortcode->getParameters()),
$this->renderer
);
try {
return $this->fragmentHandler->render(
new ControllerReference($this->controllerName, $shortcode->getParameters()),
$this->renderer
);
} catch (Throwable $exception) {
$this->logger->error('An exception was thrown when rendering the shortcode', ['exception' => $exception]);

if ($shortcode instanceof ProcessedShortcode) {
$text = trim($shortcode->getShortcodeText(), '[]');
} else {
$text = $shortcode->getName().' ...';
}

// Avoid an infinite loop that occurs if the original shortcode is returned
return "<code>&#91;$text&#93;</code>";
}
}
}

0 comments on commit e47f44a

Please sign in to comment.