Skip to content

Commit

Permalink
Added better error reporting for unicode errors in sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Oct 30, 2012
1 parent 8339cb3 commit 2b885ce
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions flask/debughelpers.py
Expand Up @@ -10,6 +10,12 @@
"""


class UnexpectedUnicodeError(AssertionError, UnicodeError):
"""Raised in places where we want some better error reporting for
unexpected unicode or binary data.
"""


class DebugFilesKeyError(KeyError, AssertionError):
"""Raised from request.files during debugging. The idea is that it can
provide a better error message than just a generic KeyError/BadRequest.
Expand Down
11 changes: 11 additions & 0 deletions flask/sessions.py
Expand Up @@ -66,6 +66,14 @@ def _tag(value):
return {' d': http_date(value)}
elif isinstance(value, dict):
return dict((k, _tag(v)) for k, v in value.iteritems())
elif isinstance(value, str):
try:
return unicode(value)
except UnicodeError:
raise UnexpectedUnicodeError(u'A byte string with '
u'non-ASCII data was passed to the session system '
u'which can only store unicode strings. Consider '
u'base64 encoding your string (String was %r)' % value)
return value
return json.dumps(_tag(value), separators=(',', ':'))

Expand Down Expand Up @@ -292,3 +300,6 @@ def save_session(self, app, session, response):
response.set_cookie(app.session_cookie_name, val,
expires=expires, httponly=httponly,
domain=domain, path=path, secure=secure)


from flask.debughelpers import UnexpectedUnicodeError

0 comments on commit 2b885ce

Please sign in to comment.