Remove use of str on exceptions

An exception's message can be a translatable message.  If it is, the message
can contain unicode characters which will cause str to fail.

In cases where the message is explicity needed, the use of str is replaced
with the use of six.text_type.  When an exception is used as a replacement
string in a format string, the logger correctly handles it without the
need for str, so it is removed.

In addition to the case where a translatable message contains unicode,
enabling lazy translation in the oslo.i18n library causes translatable
messages to be replaced with an object that does not support str, this
causes all calls to str on a translatable message to fail.  Thus this patch
is also needed to support blueprint: i18n-enablement.

This patch includes a hacking check for use of str() on exceptions identified
in except statements.

Change-Id: Idb426d7f710ea69b835f70d0e883e93e9b9111d2
Partially-Implements: blueprint i18n-enablement
This commit is contained in:
James Carey
2014-08-15 21:04:46 +00:00
committed by Pádraig Brady
parent c3ad01b65f
commit 298b0328d2
41 changed files with 229 additions and 59 deletions

View File

@@ -15,6 +15,7 @@
import os.path
import traceback
import six
import webob
from webob import exc
@@ -325,7 +326,7 @@ class AdminActionsController(wsgi.Controller):
disk_over_commit = strutils.bool_from_string(disk_over_commit,
strict=True)
except ValueError as err:
raise exc.HTTPBadRequest(explanation=str(err))
raise exc.HTTPBadRequest(explanation=six.text_type(err))
try:
instance = self.compute_api.get(context, id, want_objects=True)

View File

@@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import six
import webob
from nova.api.openstack import extensions
@@ -83,7 +84,7 @@ class AssistedVolumeSnapshotsController(wsgi.Controller):
delete_info = jsonutils.loads(delete_metadata['delete_info'])
volume_id = delete_info['volume_id']
except (KeyError, ValueError) as e:
raise webob.exc.HTTPBadRequest(explanation=str(e))
raise webob.exc.HTTPBadRequest(explanation=six.text_type(e))
try:
self.compute_api.volume_snapshot_delete(context, volume_id,

View File

@@ -419,7 +419,7 @@ class Controller(object):
try:
deleted = strutils.bool_from_string(deleted, strict=True)
except ValueError as err:
raise exc.HTTPBadRequest(explanation=str(err))
raise exc.HTTPBadRequest(explanation=six.text_type(err))
if updated_since:
try:
timeutils.parse_isotime(updated_since)

View File

@@ -14,6 +14,7 @@
import netaddr
from oslo.config import cfg
import six
import webob.exc
from nova.api.openstack import extensions
@@ -147,7 +148,7 @@ class FloatingIPBulkController(object):
else:
return net.iter_hosts()
except netaddr.AddrFormatError as exc:
raise exception.InvalidInput(reason=str(exc))
raise exception.InvalidInput(reason=six.text_type(exc))
class Floating_ips_bulk(extensions.ExtensionDescriptor):

View File

@@ -183,7 +183,7 @@ class NetworkController(wsgi.Controller):
" with project %(project)s: %(message)s") %
{"network": network_id or "",
"project": project_id,
"message": getattr(ex, "value", str(ex))})
"message": getattr(ex, "value", ex)})
raise exc.HTTPBadRequest(explanation=msg)
return webob.Response(status_int=202)

View File

@@ -17,6 +17,7 @@
import netaddr
import netaddr.core as netexc
from oslo.config import cfg
import six
import webob
from webob import exc
@@ -136,7 +137,7 @@ class NetworkController(object):
self.network_api.delete(context, id)
except exception.PolicyNotAuthorized as e:
_rollback_quota(reservation)
raise exc.HTTPForbidden(explanation=str(e))
raise exc.HTTPForbidden(explanation=six.text_type(e))
except exception.NetworkInUse as e:
_rollback_quota(reservation)
raise exc.HTTPConflict(explanation=e.format_message())
@@ -196,7 +197,7 @@ class NetworkController(object):
if CONF.enable_network_quota:
QUOTAS.commit(context, reservation)
except exception.PolicyNotAuthorized as e:
raise exc.HTTPForbidden(explanation=str(e))
raise exc.HTTPForbidden(explanation=six.text_type(e))
except Exception:
if CONF.enable_network_quota:
QUOTAS.rollback(context, reservation)

View File

@@ -278,7 +278,7 @@ class ServersController(wsgi.Controller):
if not strutils.bool_from_string(all_tenants, True):
del search_opts['all_tenants']
except ValueError as err:
raise exception.InvalidInput(str(err))
raise exception.InvalidInput(six.text_type(err))
if 'all_tenants' in search_opts:
policy.enforce(context, 'compute:get_all_tenants',

View File

@@ -582,7 +582,7 @@ class Controller(wsgi.Controller):
if not strutils.bool_from_string(all_tenants, True):
del search_opts['all_tenants']
except ValueError as err:
raise exception.InvalidInput(str(err))
raise exception.InvalidInput(six.text_type(err))
if 'all_tenants' in search_opts:
policy.enforce(context, 'compute:get_all_tenants',

View File

@@ -993,4 +993,4 @@ def safe_minidom_parse_string(xml_string):
# NOTE(Vijaya Erukala): XML input such as
# <?xml version="1.0" encoding="TF-8"?>
# raises LookupError: unknown encoding: TF-8
raise exception.MalformedRequestBody(reason=str(e))
raise exception.MalformedRequestBody(reason=six.text_type(e))