diff options
| author | Eoghan Glynn <eglynn@redhat.com> | 2012-07-19 11:42:15 +0100 |
|---|---|---|
| committer | Eoghan Glynn <eglynn@redhat.com> | 2012-07-19 11:42:15 +0100 |
| commit | 7d8936cc802ce28a476e30a6b94ab23881580b48 (patch) | |
| tree | d18fb75208750bb0bc2fa5599322628474f4884e /nova/api | |
| parent | 5f45eadbf2c1ea4a4cf6c9358aac269ca3f2cff6 (diff) | |
Static FaultWrapper status_to_type map.
Avoid maintaining needless copies of this dict, by instantiating
lazily once and only once.
This approach will bring master into sync with the corresponding
patch proposed to stable/essex:
https://review.openstack.org/9446
Change-Id: I8a7cd5fc4fe0effd436e91a6c481df7b0d5a8b01
Diffstat (limited to 'nova/api')
| -rw-r--r-- | nova/api/openstack/__init__.py | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/nova/api/openstack/__init__.py b/nova/api/openstack/__init__.py index f49c721ff..c1ce7ee94 100644 --- a/nova/api/openstack/__init__.py +++ b/nova/api/openstack/__init__.py @@ -37,18 +37,21 @@ LOG = logging.getLogger(__name__) class FaultWrapper(base_wsgi.Middleware): """Calls down the middleware stack, making exceptions into faults.""" - def __init__(self, application): - self.status_to_type = {} - for clazz in utils.walk_class_hierarchy(webob.exc.HTTPError): - self.status_to_type[clazz.code] = clazz - super(FaultWrapper, self).__init__(application) + _status_to_type = {} + + @staticmethod + def status_to_type(status): + if not FaultWrapper._status_to_type: + for clazz in utils.walk_class_hierarchy(webob.exc.HTTPError): + FaultWrapper._status_to_type[clazz.code] = clazz + return FaultWrapper._status_to_type.get( + status, webob.exc.HTTPInternalServerError)() def _error(self, inner, req, headers=None, status=500, safe=False): LOG.exception(_("Caught error: %s"), unicode(inner)) msg_dict = dict(url=req.url, status=status) LOG.info(_("%(url)s returned with HTTP %(status)d") % msg_dict) - outer = self.status_to_type.get(status, - webob.exc.HTTPInternalServerError)() + outer = self.status_to_type(status) if headers: outer.headers = headers # NOTE(johannes): We leave the explanation empty here on |
