1
0
mirror of https://github.com/flarum/core.git synced 2025-10-29 06:26:17 +01:00

Handle post rendering errors to avoid bricking (#3061)

Whether it's due to corrupted content, missing tags, caching issues, or other assorted reasons, post content can't be rendered. Currently, this results in an exception that crashes the entire forum and is hard to debug. Instead, we should log the error and show an indicator message that rendering has failed.

Co-authored-by: Sami Mazouz <sychocouldy@gmail.com>
Co-authored-by: David Wheatley <hi@davwheat.dev>
This commit is contained in:
Alexander Skvortsov
2021-10-14 14:30:18 -04:00
committed by GitHub
parent 82d67919bb
commit 60f0ef0bd5
6 changed files with 116 additions and 1 deletions

View File

@@ -9,12 +9,30 @@
namespace Flarum\Api\Serializer;
use Exception;
use Flarum\Foundation\ErrorHandling\LogReporter;
use Flarum\Post\CommentPost;
use Flarum\Post\Post;
use InvalidArgumentException;
use Symfony\Contracts\Translation\TranslatorInterface;
class BasicPostSerializer extends AbstractSerializer
{
/**
* @var LogReporter
*/
protected $log;
/**
* @var TranslatorInterface
*/
protected $translator;
public function __construct(LogReporter $log, TranslatorInterface $translator)
{
$this->log = $log;
$this->translator = $translator;
}
/**
* {@inheritdoc}
*/
@@ -41,7 +59,14 @@ class BasicPostSerializer extends AbstractSerializer
];
if ($post instanceof CommentPost) {
$attributes['contentHtml'] = $post->formatContent($this->request);
try {
$attributes['contentHtml'] = $post->formatContent($this->request);
$attributes['renderFailed'] = false;
} catch (Exception $e) {
$attributes['contentHtml'] = $this->translator->trans('core.lib.error.render_failed_message');
$this->log->report($e);
$attributes['renderFailed'] = true;
}
} else {
$attributes['content'] = $post->content;
}