Ticket #2250 (closed defect: fixed)
[PATCH] Repoze.what cannot handle non-ascii error messages
| Reported by: | chrisz | Owned by: | Gustavo |
|---|---|---|---|
| Priority: | high | Milestone: | 2.0rc1 |
| Component: | TurboGears | Version: | 2.0b6 |
| Severity: | major | Keywords: | repoze.what |
| Cc: |
Description
Steps to reproduce the problem:
- Quickstart a TG2 project with authorization
- Setup the database and run the project with --reload
- Surf to http://localhost:8080/manage_permission_only
- You get an error message "Only for managers"
- In the require decorator of the controllers.root.manage_permission_only method, set msg=l_(u'Nur für Mänätscher'), i.e. put some non-ascii chars in the error message (make sure you have an encoding hint at the top of the file).
- Surf to http://localhost:8080/manage_permission_only
- You get an UnicodeEncodeError
Cause: The error message cannot be converted to unicode
Solution: In repoze.what.predicates, add the following method to the PredicateError class:
class PredicateError(Exception):
def __unicode__(self):
return unicode(self.message)
Change History
comment:1 in reply to: ↑ description Changed 3 years ago by Gustavo
- Owner set to Gustavo
- Status changed from new to assigned
comment:2 Changed 3 years ago by Gustavo
- Status changed from assigned to closed
- Resolution set to invalid
Unfortunately, Exception.message is available as of Py 2.5 (although deprecated in Py 2.6), so Py 2.4 users will get AttributeError exceptions even if the message contains ASCII chars only. :(
Thanks anyway! ;-)
comment:3 follow-up: ↓ 4 Changed 3 years ago by chrisz
- Status changed from closed to reopened
- Resolution invalid deleted
I'm reopening this because I consider it very important - my users really want localized error messages.
So how about the following patch that should work with Py 2.4-2.6 (it does nothing with Py 2.6):
class PredicateError(Exception):
if not hasattr(Exception, '__unicode__'):
def __unicode__(self):
return unicode(self.args and self.args[0] or '')
comment:4 in reply to: ↑ 3 ; follow-up: ↓ 5 Changed 3 years ago by Gustavo
Replying to chrisz:
I'm reopening this because I consider it very important - my users really want localized error messages.
Localized error messages are supported, the only problem is when they are logged in Py < 2.6.
So how about the following patch that should work with Py 2.4-2.6 (it does nothing with Py 2.6):
class PredicateError(Exception): if not hasattr(Exception, '__unicode__'): def __unicode__(self): return unicode(self.args and self.args[0] or '')
Yes, it does seem to work. I'll try to verify it and apply it today.
comment:5 in reply to: ↑ 4 Changed 3 years ago by chrisz
Replying to Gustavo:
Localized error messages are supported, the only problem is when they are logged in Py < 2.6.
But by default they *are* logged in the TG2 development environment.
And besides the logging, there is also another problem with the ActionProtector class in repoze.what.pylons which contains the following code
except NotAuthorizedError, e:
reason = unicode(e)
Without the patch, this fails for Py < 2.6.
We were already aware of this issue, but it's a bug in Python itself (in its logging package). It's fixed in Python 2.6.
I'll check this to see if it's a valid workaround. Thanks.