def test_addr_format_error_exception(self): req = wsgi_resource.Request({}) language = req.best_match_language() e = netaddr.AddrFormatError() result = common.convert_exception_to_http_exc(e, {}, language) except_res = {'message': '', 'type': 'AddrFormatError', 'detail': ''} self.assertEqual(except_res, json.loads(result.body)["TackerError"]) self.assertEqual(500, result.code)
def test_not_implemented_error_exception(self): req = wsgi_resource.Request({}) language = req.best_match_language() e = NotImplementedError() result = common.convert_exception_to_http_exc(e, {}, language) except_res = {'NotImplementedError': {'message': '', 'type': 'NotImplementedError', 'detail': ''}} self.assertEqual(except_res, jsonutils.loads(result.body)) self.assertEqual(501, result.code)
def test_policy_not_authorized_exception(self): req = wsgi_resource.Request({}) language = req.best_match_language() e = oslo_policy.PolicyNotAuthorized(None, None, None) result = common.convert_exception_to_http_exc(e, {}, language) except_res = {'message': 'None is disallowed by policy', 'type': 'PolicyNotAuthorized', 'detail': ''} self.assertEqual( except_res, jsonutils.loads(result.body)["TackerError"]) self.assertEqual(500, result.code)
def test_tacker_exception(self): req = wsgi_resource.Request({}) language = req.best_match_language() e = exceptions.TackerException() result = common.convert_exception_to_http_exc(e, {}, language) except_res = {'message': 'An unknown exception occurred.', 'type': 'TackerException', 'detail': ''} self.assertEqual( except_res, jsonutils.loads(result.body)["TackerError"]) self.assertEqual(500, result.code)
def resource(request): route_args = request.environ.get('wsgiorg.routing_args') if route_args: args = route_args[1].copy() else: args = {} # NOTE(jkoelker) by now the controller is already found, remove # it from the args if it is in the matchdict args.pop('controller', None) fmt = args.pop('format', None) action = args.pop('action', None) content_type = format_types.get(fmt, request.best_match_content_type()) language = request.best_match_language() deserializer = deserializers.get(content_type) serializer = serializers.get(content_type) try: if request.body: args['body'] = deserializer.deserialize(request.body)['body'] method = getattr(controller, action) result = method(request=request, **args) except Exception as e: mapped_exc = api_common.convert_exception_to_http_exc( e, faults, language) if hasattr(mapped_exc, 'code') and 400 <= mapped_exc.code < 500: LOG.info('%(action)s failed (client error): %(exc)s', { 'action': action, 'exc': mapped_exc }) else: LOG.exception('%(action)s failed: %(details)s', { 'action': action, 'details': extract_exc_details(e) }) raise mapped_exc status = action_status.get(action, 200) body = serializer.serialize(result) # NOTE(jkoelker) Comply with RFC2616 section 9.7 if status == 204: content_type = '' body = None return webob.Response(request=request, status=status, content_type=content_type, body=body)
def test_http_exception(self): req = wsgi_resource.Request({}) language = req.best_match_language() e = exc.HTTPException result = common.convert_exception_to_http_exc(e, {}, language) except_res = {"message": "Request Failed: internal server error " "while processing your request.", "type": "HTTPInternalServerError", "detail": ""} self.assertEqual( except_res, jsonutils.loads(result.body)["TackerError"]) self.assertEqual(500, result.code)
def test_http_client_exception(self): req = wsgi_resource.Request({}) language = req.best_match_language() e = exc.HTTPClientError() result = common.convert_exception_to_http_exc(e, {}, language) except_res = {'message': 'The server could not comply with' ' the request since it is either ' 'malformed or otherwise incorrect.', 'type': 'HTTPClientError', 'detail': ''} self.assertEqual( except_res, jsonutils.loads(result.body)["TackerError"]) self.assertEqual(400, result.code)
def resource(request): route_args = request.environ.get('wsgiorg.routing_args') if route_args: args = route_args[1].copy() else: args = {} # NOTE(jkoelker) by now the controller is already found, remove # it from the args if it is in the matchdict args.pop('controller', None) fmt = args.pop('format', None) action = args.pop('action', None) content_type = format_types.get(fmt, request.best_match_content_type()) language = request.best_match_language() deserializer = deserializers.get(content_type) serializer = serializers.get(content_type) try: if request.body: args['body'] = deserializer.deserialize(request.body)['body'] method = getattr(controller, action) result = method(request=request, **args) except Exception as e: mapped_exc = api_common.convert_exception_to_http_exc(e, faults, language) if hasattr(mapped_exc, 'code') and 400 <= mapped_exc.code < 500: LOG.info('%(action)s failed (client error): %(exc)s', {'action': action, 'exc': mapped_exc}) else: LOG.exception('%(action)s failed: %(details)s', {'action': action, 'details': extract_exc_details(e)}) raise mapped_exc status = action_status.get(action, 200) body = serializer.serialize(result) # NOTE(jkoelker) Comply with RFC2616 section 9.7 if status == 204: content_type = '' body = None return webob.Response(request=request, status=status, content_type=content_type, body=body)