Allow passing a json_error_formatter to the middleware
This is so error responses can be formatted according to the rules of the application. Change-Id: Ie44399447a349ebc49251dc63c23f56c6423fca6
This commit is contained in:
parent
b5359fcd80
commit
f816e324b8
@ -104,6 +104,12 @@ versions_list
|
|||||||
It's assumed that any application that is using microversions will have such
|
It's assumed that any application that is using microversions will have such
|
||||||
a list for its own housekeeping and documentation.
|
a list for its own housekeeping and documentation.
|
||||||
|
|
||||||
|
One named parameter is optional:
|
||||||
|
|
||||||
|
json_error_formatter
|
||||||
|
A Webob error formatter that can be used to structure the response when JSON
|
||||||
|
is expected.
|
||||||
|
|
||||||
For example::
|
For example::
|
||||||
|
|
||||||
def app():
|
def app():
|
||||||
|
@ -37,7 +37,8 @@ class MicroversionMiddleware(object):
|
|||||||
Otherwise the application is called.
|
Otherwise the application is called.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, application, service_type, versions):
|
def __init__(self, application, service_type, versions,
|
||||||
|
json_error_formatter=None):
|
||||||
"""Create the WSGI middleware.
|
"""Create the WSGI middleware.
|
||||||
|
|
||||||
:param application: The application hosting the service.
|
:param application: The application hosting the service.
|
||||||
@ -45,11 +46,14 @@ class MicroversionMiddleware(object):
|
|||||||
of the application.
|
of the application.
|
||||||
:param versions: An ordered list of legitimate versions for the
|
:param versions: An ordered list of legitimate versions for the
|
||||||
application.
|
application.
|
||||||
|
:param json_error_formatter: A Webob exception error formatter.
|
||||||
|
See Webob for details.
|
||||||
"""
|
"""
|
||||||
self.application = application
|
self.application = application
|
||||||
self.service_type = service_type
|
self.service_type = service_type
|
||||||
self.microversion_environ = '%s.microversion' % service_type
|
self.microversion_environ = '%s.microversion' % service_type
|
||||||
self.versions = versions
|
self.versions = versions
|
||||||
|
self.json_error_formatter = json_error_formatter
|
||||||
|
|
||||||
@webob.dec.wsgify
|
@webob.dec.wsgify
|
||||||
def __call__(self, req):
|
def __call__(self, req):
|
||||||
@ -57,13 +61,16 @@ class MicroversionMiddleware(object):
|
|||||||
microversion = microversion_parse.extract_version(
|
microversion = microversion_parse.extract_version(
|
||||||
req.headers, self.service_type, self.versions)
|
req.headers, self.service_type, self.versions)
|
||||||
# TODO(cdent): These error response are not formatted according to
|
# TODO(cdent): These error response are not formatted according to
|
||||||
# api-sig guidelines.
|
# api-sig guidelines, unless a json_error_formatter is provided
|
||||||
|
# that can do it. For an example, see the placement service.
|
||||||
except ValueError as exc:
|
except ValueError as exc:
|
||||||
raise webob.exc.HTTPNotAcceptable(
|
raise webob.exc.HTTPNotAcceptable(
|
||||||
('Invalid microversion: %(error)s') % {'error': exc})
|
('Invalid microversion: %(error)s') % {'error': exc},
|
||||||
|
json_formatter=self.json_error_formatter)
|
||||||
except TypeError as exc:
|
except TypeError as exc:
|
||||||
raise webob.exc.HTTPBadRequest(
|
raise webob.exc.HTTPBadRequest(
|
||||||
('Invalid microversion: %(error)s') % {'error': exc})
|
('Invalid microversion: %(error)s') % {'error': exc},
|
||||||
|
json_formatter=self.json_error_formatter)
|
||||||
|
|
||||||
req.environ[self.microversion_environ] = microversion
|
req.environ[self.microversion_environ] = microversion
|
||||||
microversion_header = '%s %s' % (self.service_type, microversion)
|
microversion_header = '%s %s' % (self.service_type, microversion)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user