From fa5deb6b44b61b3c501008797b08afcb5a1b9426 Mon Sep 17 00:00:00 2001 From: Mukesh Date: Sun, 30 Dec 2018 12:02:28 +0530 Subject: [PATCH] More informative error for invalid values passed to `|richtext` (#4971) Fixes #4958 --- CHANGELOG.txt | 1 + CONTRIBUTORS.rst | 1 + docs/releases/2.5.rst | 2 ++ wagtail/core/templatetags/wagtailcore_tags.py | 5 ++++- wagtail/core/tests/tests.py | 8 ++++++++ 5 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index f2a271edd4..9cb19d1015 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -4,6 +4,7 @@ Changelog 2.5 (xx.xx.xxxx) - IN DEVELOPMENT ~~~~~~~~~~~~~~~~ + * Added more informative error message when `|richtext` filter is applied to a non-string value (mukesh5) * Fix: Set `SERVER_PORT` to 443 in `Page.dummy_request()` for HTTPS sites (Sergey Fedoseev) * Fix: Validation error messages in `InlinePanel` no longer count towards `max_num` when disabling the 'add' button (Todd Dembrey, Thibaud Colas) diff --git a/CONTRIBUTORS.rst b/CONTRIBUTORS.rst index 7882c8d3b2..b2c6228a0f 100644 --- a/CONTRIBUTORS.rst +++ b/CONTRIBUTORS.rst @@ -337,6 +337,7 @@ Contributors * Nick Travis * Maylon Pedroso * Thijs Walcarius +* mukesh5 Translators =========== diff --git a/docs/releases/2.5.rst b/docs/releases/2.5.rst index 9363d129f2..1cda5d3b5c 100644 --- a/docs/releases/2.5.rst +++ b/docs/releases/2.5.rst @@ -14,6 +14,8 @@ What's new Other features ~~~~~~~~~~~~~~ + * Added more informative error message when ``|richtext`` filter is applied to a non-string value (mukesh5) + Bug fixes ~~~~~~~~~ diff --git a/wagtail/core/templatetags/wagtailcore_tags.py b/wagtail/core/templatetags/wagtailcore_tags.py index b14de2345b..dda1cf62d8 100644 --- a/wagtail/core/templatetags/wagtailcore_tags.py +++ b/wagtail/core/templatetags/wagtailcore_tags.py @@ -87,7 +87,10 @@ def richtext(value): elif value is None: html = '' else: - html = expand_db_html(value) + if isinstance(value, str): + html = expand_db_html(value) + else: + raise TypeError("'richtext' template filter received an invalid value; expected string, got {}.".format(type(value))) return mark_safe('
' + html + '
') diff --git a/wagtail/core/tests/tests.py b/wagtail/core/tests/tests.py index e70eacb97c..61507fe895 100644 --- a/wagtail/core/tests/tests.py +++ b/wagtail/core/tests/tests.py @@ -261,3 +261,11 @@ class TestRichtextTag(TestCase): def test_call_with_none(self): result = richtext(None) self.assertEqual(result, '
') + + def test_call_with_invalid_value(self): + with self.assertRaisesRegex(TypeError, "'richtext' template filter received an invalid value"): + richtext(42) + + def test_call_with_bytes(self): + with self.assertRaisesRegex(TypeError, "'richtext' template filter received an invalid value"): + richtext(b"Hello world!")