Beispiel #1
0
 def test_attach_exc_details_with_dict_interpolation(self):
     e = Exception()
     utils.attach_exc_details(
         e, 'details: %(foo)s, %(bar)s', {'foo': 'foo', 'bar': 'bar'})
     self.assertEqual('details: foo, bar', utils.extract_exc_details(e))
Beispiel #2
0
 def test_extract_exc_details_no_details_attached(self):
     self.assertIsInstance(
         utils.extract_exc_details(Exception()), six.text_type)
Beispiel #3
0
 def test_attach_exc_details_with_None_interpolation(self):
     e = Exception()
     utils.attach_exc_details(e, 'details: %s', None)
     self.assertEqual(
         'details: %s' % str(None), utils.extract_exc_details(e))
Beispiel #4
0
 def test_attach_exc_details_with_multiple_interpolation(self):
     e = Exception()
     utils.attach_exc_details(
         e, 'details: %s, %s', ('foo', 'bar'))
     self.assertEqual('details: foo, bar', utils.extract_exc_details(e))
Beispiel #5
0
 def test_attach_exc_details(self):
     e = Exception()
     utils.attach_exc_details(e, 'details')
     self.assertEqual('details', utils.extract_exc_details(e))
Beispiel #6
0
 def test_attach_exc_details_with_interpolation(self):
     e = Exception()
     utils.attach_exc_details(e, 'details: %s', 'foo')
     self.assertEqual('details: foo', utils.extract_exc_details(e))
Beispiel #7
0
 def test_attach_exc_details_with_interpolation(self):
     e = Exception()
     utils.attach_exc_details(e, 'details: %s', 'foo')
     self.assertEqual('details: foo', utils.extract_exc_details(e))
Beispiel #8
0
 def test_attach_exc_details_with_None_interpolation(self):
     e = Exception()
     utils.attach_exc_details(e, 'details: %s', None)
     self.assertEqual('details: %s' % str(None),
                      utils.extract_exc_details(e))
Beispiel #9
0
 def test_extract_exc_details_no_details_attached(self):
     self.assertIsInstance(utils.extract_exc_details(Exception()),
                           six.text_type)
Beispiel #10
0
 def test_attach_exc_details(self):
     e = Exception()
     utils.attach_exc_details(e, 'details')
     self.assertEqual('details', utils.extract_exc_details(e))
Beispiel #11
0
 def test_attach_exc_details_with_multiple_interpolation(self):
     e = Exception()
     utils.attach_exc_details(e, 'details: %s, %s', ('foo', 'bar'))
     self.assertEqual('details: foo, bar', utils.extract_exc_details(e))
Beispiel #12
0
    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']

            # Routes library is dumb and cuts off everything after last dot (.)
            # as format. At the same time, it doesn't enforce format suffix,
            # which combined makes it impossible to pass a 'id' with dots
            # included (the last section after the last dot is lost). This is
            # important for some API extensions like tags where the id is
            # really a tag name that can contain special characters.
            #
            # To work around the Routes behaviour, we will attach the suffix
            # back to id if it's not one of supported formats (atm json only).
            # This of course won't work for the corner case of a tag name that
            # actually ends with '.json', but there seems to be no better way
            # to tackle it without breaking API backwards compatibility.
            if fmt is not None and fmt not in format_types:
                args['id'] = '.'.join([args['id'], fmt])

            revision_number = api_common.check_request_for_revision_constraint(
                request)
            if revision_number is not None:
                request.context.set_transaction_constraint(
                    controller._collection, args['id'], revision_number)

            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': utils.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)
Beispiel #13
0
    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']

            # Routes library is dumb and cuts off everything after last dot (.)
            # as format. At the same time, it doesn't enforce format suffix,
            # which combined makes it impossible to pass a 'id' with dots
            # included (the last section after the last dot is lost). This is
            # important for some API extensions like tags where the id is
            # really a tag name that can contain special characters.
            #
            # To work around the Routes behaviour, we will attach the suffix
            # back to id if it's not one of supported formats (atm json only).
            # This of course won't work for the corner case of a tag name that
            # actually ends with '.json', but there seems to be no better way
            # to tackle it without breaking API backwards compatibility.
            if fmt is not None and fmt not in format_types:
                args['id'] = '.'.join([args['id'], fmt])

            revision_number = api_common.check_request_for_revision_constraint(
                request)
            if revision_number is not None:
                request.context.set_transaction_constraint(
                    controller._collection, args['id'], revision_number)

            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': utils.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)
Beispiel #14
0
 def test_attach_exc_details_with_dict_interpolation(self):
     e = Exception()
     utils.attach_exc_details(
         e, 'details: %(foo)s, %(bar)s', {'foo': 'foo', 'bar': 'bar'})
     self.assertEqual('details: foo, bar', utils.extract_exc_details(e))
Beispiel #15
0
    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(_LI("%(action)s failed (client error): %(exc)s"), {"action": action, "exc": mapped_exc})
            else:
                LOG.exception(
                    _LE("%(action)s failed: %(details)s"), {"action": action, "details": utils.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)