bugdown: Raise BugdownRenderingException.

We now raise an exception in bugdown.do_convert() if rendering
fails, to avoid silent failures, and then calling code can convert
the exception to a JsonableError.
This commit is contained in:
Steve Howell
2016-09-15 13:23:25 -07:00
committed by Tim Abbott
parent de25f07961
commit a036a72db6
3 changed files with 10 additions and 4 deletions

View File

@@ -9,6 +9,7 @@ from django.utils.translation import ugettext as _
from django.conf import settings
from django.core import validators
from django.contrib.sessions.models import Session
from zerver.lib.bugdown import BugdownRenderingException
from zerver.lib.cache import flush_user_profile
from zerver.lib.context_managers import lockfile
from zerver.models import Realm, RealmEmoji, Stream, UserProfile, UserActivity, \
@@ -1064,7 +1065,9 @@ def check_message(sender, client, message_type_name, message_to,
message.pub_date = timezone.now()
message.sending_client = client
if not message.maybe_render_content(realm.domain):
try:
message.maybe_render_content(realm.domain)
except BugdownRenderingException:
raise JsonableError(_("Unable to render message"))
if client.name == "zephyr_mirror":

View File

@@ -57,6 +57,9 @@ if False:
# mypy requires the Optional to be inside Union
ElementStringNone = Union[Element, Optional[text_type]]
class BugdownRenderingException(Exception):
pass
def unescape(s):
# type: (text_type) -> (text_type)
if six.PY2:
@@ -1177,7 +1180,7 @@ def do_convert(md, realm_domain=None, message=None):
mail.mail_admins(subject, "Failed message: %s\n\n%s\n\n" % (
cleaned, traceback.format_exc()),
fail_silently=False)
return None
raise BugdownRenderingException()
finally:
current_message = None
db_data = None

View File

@@ -570,8 +570,8 @@ class BugdownErrorTests(ZulipTestCase):
def test_bugdown_error_handling(self):
# type: () -> None
with self.simulated_markdown_failure():
rendered_message = bugdown.convert('', 'zulip.com')
self.assertEqual(rendered_message, None)
with self.assertRaises(bugdown.BugdownRenderingException):
bugdown.convert('', 'zulip.com')
def test_send_message_errors(self):
# type: () -> None