Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle post rendering errors to avoid bricking #3061

Merged
merged 11 commits into from
Oct 14, 2021
2 changes: 2 additions & 0 deletions js/src/common/models/Post.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ Object.assign(Post.prototype, {

createdAt: Model.attribute('createdAt', Model.transformDate),
user: Model.hasOne('user'),

contentType: Model.attribute('contentType'),
content: Model.attribute('content'),
contentHtml: Model.attribute('contentHtml'),
renderFailed: Model.attribute('renderFailed'),
contentPlain: computed('contentHtml', getPlainContent),

editedAt: Model.attribute('editedAt', Model.transformDate),
Expand Down
1 change: 1 addition & 0 deletions js/src/forum/components/CommentPost.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export default class CommentPost extends Post {
' ' +
classList({
CommentPost: true,
'Post--renderFailed': post.renderFailed(),
'Post--hidden': post.isHidden(),
'Post--edited': post.isEdited(),
revealContent: this.revealContent,
Expand Down
4 changes: 4 additions & 0 deletions less/forum/Post.less
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@
}
}

.Post--renderFailed {
background-color: @control-danger-color;
askvortsov1 marked this conversation as resolved.
Show resolved Hide resolved
}

.Post--hidden {
.Post-header, .Post-header a, .PostUser h3, .PostUser h3 a {
color: @muted-more-color;
Expand Down
1 change: 1 addition & 0 deletions locale/core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ core:
not_found_message: The requested resource was not found.
permission_denied_message: You do not have permission to do that.
rate_limit_exceeded_message: You're going a little too quickly. Please try again in a few seconds.
render_failed_message: The formatter was unable to format this content due to an error. Please check the Flarum error logs for more information.
askvortsov1 marked this conversation as resolved.
Show resolved Hide resolved

# These translations are used in the loading indicator component.
loading_indicator:
Expand Down
12 changes: 11 additions & 1 deletion src/Api/Serializer/BasicPostSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@

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
{
Expand Down Expand Up @@ -41,7 +44,14 @@ protected function getDefaultAttributes($post)
];

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'] = resolve(TranslatorInterface::class)->trans('core.lib.error.render_failed_message');
resolve(LogReporter::class)->report($e);
askvortsov1 marked this conversation as resolved.
Show resolved Hide resolved
$attributes['renderFailed'] = true;
}
} else {
$attributes['content'] = $post->content;
}
Expand Down