From 3574a15e8c9448b17f5c090666099c3d46804d4a Mon Sep 17 00:00:00 2001 From: Thomas NOEL Date: Wed, 29 Aug 2018 23:08:30 +0200 Subject: [PATCH] handle non-ascii syntax error messages on conditions (#25954) --- tests/test_api.py | 3 +++ wcs/conditions.py | 5 +++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/test_api.py b/tests/test_api.py index 14680d17..ed65066a 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -2092,6 +2092,9 @@ def test_validate_condition(pub): assert resp.json['klass'] == 'error' assert 'Python condition cannot contain {{' in resp.json['msg'] + resp = get_app(pub).get('/api/validate-condition?type=django&value_django=un+%C3%A9l%C3%A9phant') + assert resp.json['klass'] == 'error' + assert resp.json['msg'].startswith(u"syntax error: Unused 'éléphant'") resp = get_app(pub).get('/api/validate-condition?type=django&value_django=~2') assert resp.json['klass'] == 'error' assert resp.json['msg'].startswith('syntax error') diff --git a/wcs/conditions.py b/wcs/conditions.py index a2b6ebc8..bbe37804 100644 --- a/wcs/conditions.py +++ b/wcs/conditions.py @@ -20,6 +20,7 @@ from quixote import get_publisher from django.template import Context, Template, TemplateSyntaxError from qommon import _, get_logger +from qommon.misc import site_encode class ValidationError(ValueError): @@ -86,11 +87,11 @@ class Condition(object): try: compile(self.value, '', 'eval') except (SyntaxError, TypeError) as e: - raise ValidationError(_('syntax error: %s') % e) + raise ValidationError(_('syntax error: %s') % site_encode(e)) def validate_django(self): try: Template('{%% load %s %%}{%% if %s %%}OK{%% endif %%}' % ( get_publisher().get_default_templatetags_libraries(), self.value)) except TemplateSyntaxError as e: - raise ValidationError(_('syntax error: %s') % e) + raise ValidationError(_('syntax error: %s') % site_encode(e)) -- 2.18.0