From b5a314de760e3e4809cc0056ab4af2422e71a775 Mon Sep 17 00:00:00 2001 From: Alessandro Molina Date: Thu, 19 Jul 2012 00:06:51 +0200 Subject: [PATCH] avoid issues with unicode error messages --- tests/test_validation.py | 10 ++++++++++ tw2/core/validation.py | 5 +++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/test_validation.py b/tests/test_validation.py index 289a681..99d6187 100644 --- a/tests/test_validation.py +++ b/tests/test_validation.py @@ -78,6 +78,16 @@ def test_catch_errors(self): except twc.ValidationError: pass + def test_unicode_catch_errors(self): + try: + formencode.api.set_stdtranslation(languages=['tr']) + twc.validation.catch_errors(lambda s, x: formencode.validators.Int.to_python(x))(None, 'x') + assert(False) + except twc.ValidationError: + pass + finally: + formencode.api.set_stdtranslation(languages=['en']) + def test_unflatten(self): assert(twc.validation.unflatten_params({'a':1, 'b:c':2}) == {'a':1, 'b':{'c':2}}) diff --git a/tw2/core/validation.py b/tw2/core/validation.py index 71cca8a..947ab36 100644 --- a/tw2/core/validation.py +++ b/tw2/core/validation.py @@ -94,9 +94,10 @@ def wrapper(self, *args, **kw): d = fn(self, *args, **kw) return d except catch, e: + e_msg = unicode(e) if self: - self.error_msg = str(e) - raise ValidationError(str(e), widget=self) + self.error_msg = e_msg + raise ValidationError(e_msg, widget=self) return wrapper