Skip to content

Commit

Permalink
Fail gracefully when the db doesn't speak unicode
Browse files Browse the repository at this point in the history
When sqlalchemy/mysql doesn't contain the charset=utf8 or use_unicode=1
parameters (and the default mysql connection charset is not utf8),
sqlalchemy will connect to mysql in ascii mode; In ascii mode, it will
attempt to reencode all input data to latin-1 regardless of the
database/table collation setting. This catches that error and turns it
into an invalid parameter.

(The error message associated with this is something like
UnicodeEncodeError: 'latin-1' codec can't encode character u'\u2026' in
position 30: ordinal not in range(256))

This fixes bug 944034 and is related to bug 898808

Change-Id: I082b7568ef9e9d2104e13aa58d207535ef201bd3
  • Loading branch information
novas0x2a committed Mar 2, 2012
1 parent 2034692 commit 27013e8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions nova/api/openstack/wsgi.py
Expand Up @@ -574,6 +574,9 @@ def __exit__(self, ex_type, ex_value, ex_traceback):
if isinstance(ex_value, exception.NotAuthorized):
msg = unicode(ex_value)
raise Fault(webob.exc.HTTPForbidden(explanation=msg))
elif isinstance(ex_value, exception.Invalid):
raise Fault(exception.ConvertedException(
code=ex_value.code, explanation=unicode(ex_value)))
elif isinstance(ex_value, TypeError):
exc_info = (ex_type, ex_value, ex_traceback)
LOG.error(_('Exception handling resource: %s') % ex_value,
Expand Down
7 changes: 7 additions & 0 deletions nova/exception.py
Expand Up @@ -88,6 +88,8 @@ def wrap_db_error(f):
def _wrap(*args, **kwargs):
try:
return f(*args, **kwargs)
except UnicodeEncodeError:
raise InvalidUnicodeParameter()
except Exception, e:
LOG.exception(_('DB exception wrapped.'))
raise DBError(e)
Expand Down Expand Up @@ -275,6 +277,11 @@ class InvalidRPCConnectionReuse(Invalid):
message = _("Invalid reuse of an RPC connection.")


class InvalidUnicodeParameter(Invalid):
message = _("Invalid Parameter: "
"Unicode is not supported by the current database.")


# Cannot be templated as the error syntax varies.
# msg needs to be constructed when raised.
class InvalidParameterValue(Invalid):
Expand Down

0 comments on commit 27013e8

Please sign in to comment.