def _error(self, inner, req): if isinstance(inner, UnicodeDecodeError): msg = _("Error decoding your request. Either the URL or the " "request body contained characters that could not be " "decoded by Cinder.") return wsgi.Fault(webob.exc.HTTPBadRequest(explanation=msg)) if not isinstance(inner, exception.QuotaError): LOG.exception("Caught error: %(type)s %(error)s", {'type': type(inner), 'error': inner}) safe = getattr(inner, 'safe', False) headers = getattr(inner, 'headers', None) status = getattr(inner, 'code', http_client.INTERNAL_SERVER_ERROR) if status is None: status = http_client.INTERNAL_SERVER_ERROR msg_dict = dict(url=req.url, status=status) LOG.info("%(url)s returned with HTTP %(status)s", msg_dict) outer = self.status_to_type(status) if headers: outer.headers = headers # NOTE(johannes): We leave the explanation empty here on # purpose. It could possibly have sensitive information # that should not be returned back to the user. See # bugs 868360 and 874472 # NOTE(eglynn): However, it would be over-conservative and # inconsistent with the EC2 API to hide every exception, # including those that are safe to expose, see bug 1021373 if safe: msg = (inner.msg if isinstance(inner, exception.CinderException) else six.text_type(inner)) params = {'exception': inner.__class__.__name__, 'explanation': msg} outer.explanation = _('%(exception)s: %(explanation)s') % params return wsgi.Fault(outer)
def _error(self, inner, req): if not isinstance(inner, exception.QuotaError): LOG.exception(_LE("Caught error: %s"), unicode(inner)) safe = getattr(inner, 'safe', False) headers = getattr(inner, 'headers', None) status = getattr(inner, 'code', 500) if status is None: status = 500 msg_dict = dict(url=req.url, status=status) LOG.info(_LI("%(url)s returned with HTTP %(status)d") % msg_dict) outer = self.status_to_type(status) if headers: outer.headers = headers # NOTE(johannes): We leave the explanation empty here on # purpose. It could possibly have sensitive information # that should not be returned back to the user. See # bugs 868360 and 874472 # NOTE(eglynn): However, it would be over-conservative and # inconsistent with the EC2 API to hide every exception, # including those that are safe to expose, see bug 1021373 if safe: msg = (inner.msg if isinstance(inner, exception.CinderException) else unicode(inner)) params = {'exception': inner.__class__.__name__, 'explanation': msg} outer.explanation = _('%(exception)s: %(explanation)s') % params return wsgi.Fault(outer)
def raiser(req): class MyInvalidInput(exception.InvalidInput): message = msg_template ex = MyInvalidInput(reason=reason) raise wsgi.Fault(exception.ConvertedException(code=ex.code, explanation=ex.msg))
def test_versions_response_fault(self): version = '3.0' req = self.build_request(header_version=version) req.api_version_request = ( api_version_request.APIVersionRequest(version)) app = wsgi.Fault(webob.exc.HTTPBadRequest(explanation='what?')) response = req.get_response(app) self.assertEqual(HTTPStatus.BAD_REQUEST, response.status_int) self.check_response(response, '3.0')
def test_xml_serializer(self): """Ensure that a v2 request responds with a v2 xmlns.""" request = webob.Request.blank('/v2', headers={"Accept": "application/xml"}) fault = wsgi.Fault(webob.exc.HTTPBadRequest(explanation='scram')) response = request.get_response(fault) self.assertIn(common.XML_NS_V2, response.body.decode()) self.assertEqual("application/xml", response.content_type) self.assertEqual(400, response.status_int)
def test_xml_serializer(self): """Ensure that a v1.1 request responds with a v1 xmlns""" request = webob.Request.blank('/v1', headers={"Accept": "application/xml"}) fault = wsgi.Fault(webob.exc.HTTPBadRequest(explanation='scram')) response = request.get_response(fault) self.assertIn(common.XML_NS_V1, response.body) self.assertEqual(response.content_type, "application/xml") self.assertEqual(response.status_int, 400)
def test_versions_response_fault(self, version): req = self.build_request(header_version=version) req.api_version_request = ( api_version_request.APIVersionRequest(version)) app = wsgi.Fault(webob.exc.HTTPBadRequest(explanation='what?')) response = req.get_response(app) self.assertEqual(http_client.BAD_REQUEST, response.status_int) if version == '3.0': self.check_response(response, '3.0') else: self.assertNotIn(VERSION_HEADER_NAME, response.headers)
def test_400_fault_json(self): """Test fault serialized to JSON via file-extension and/or header.""" requests = [ webob.Request.blank('/.json'), webob.Request.blank('/', headers={"Accept": "application/json"}), ] for request in requests: fault = wsgi.Fault(webob.exc.HTTPBadRequest(explanation='scram')) response = request.get_response(fault) expected = { "badRequest": { "message": "scram", "code": 400, }, } actual = jsonutils.loads(response.body) self.assertEqual("application/json", response.content_type) self.assertEqual(expected, actual)
def test_413_fault_json(self): """Test fault serialized to JSON via file-extension and/or header.""" requests = [ webob.Request.blank('/.json'), webob.Request.blank('/', headers={"Accept": "application/json"}), ] for request in requests: exc = webob.exc.HTTPRequestEntityTooLarge fault = wsgi.Fault( exc(explanation='sorry', headers={'Retry-After': '4'})) response = request.get_response(fault) expected = { "overLimit": { "message": "sorry", "code": 413, "retryAfter": "4", }, } actual = jsonutils.loads(response.body) self.assertEqual("application/json", response.content_type) self.assertEqual(expected, actual)
def raiser(req): raise wsgi.Fault(webob.exc.HTTPNotFound(explanation='whut?'))
def test_fault_has_status_int(self): """Ensure the status_int is set correctly on faults.""" fault = wsgi.Fault(webob.exc.HTTPBadRequest(explanation='what?')) self.assertEqual(400, fault.status_int)
def raiser(req): raise wsgi.Fault(webob.exc.HTTPForbidden(explanation='whut?'))