Example #1
0
    def create(cls, error):
        """Generate a DataModelException from an existing CongressException.

        :param error has a 'name' field corresponding to an error_codes
        error-name.  It may also have a 'data' field.
        Returns a DataModelException properly populated.
        """
        name = getattr(error, "name", None)
        if name:
            error_code = error_codes.get_num(name)
            description = error_codes.get_desc(name)
            http_status_code = error_codes.get_http(name)
        else:
            # Check if it's default http error or else return 'Unknown error'
            error_code = error.code or httplib.BAD_REQUEST
            if error_code not in httplib.responses:
                error_code = httplib.BAD_REQUEST
            description = httplib.responses.get(error_code, "Unknown error")
            http_status_code = error_code

        if str(error):
            description += "::" + original_msg(error)
        return cls(error_code=error_code,
                   description=description,
                   data=getattr(error, 'data', None),
                   http_status_code=http_status_code)
Example #2
0
    def create(cls, error):
        """Generate a DataModelException from an existing CongressException.

        :param error has a 'name' field corresponding to an error_codes
        error-name.  It may also have a 'data' field.
        Returns a DataModelException properly populated.
        """
        name = getattr(error, "name", None)
        if name:
            error_code = error_codes.get_num(name)
            description = error_codes.get_desc(name)
            http_status_code = error_codes.get_http(name)
        else:
            # Check if it's default http error or else return 'Unknown error'
            error_code = error.code or httplib.BAD_REQUEST
            if error_code not in httplib.responses:
                error_code = httplib.BAD_REQUEST
            description = httplib.responses.get(error_code, "Unknown error")
            http_status_code = error_code

        if str(error):
            description += "::" + str(error)
        return cls(error_code=error_code,
                   description=description,
                   data=getattr(error, 'data', None),
                   http_status_code=http_status_code)
Example #3
0
    def create(cls, error):
        """Generate a DataModelException from an existing CongressException.

        :param error has a 'name' field corresponding to an error_codes
        error-name.  It may also have a 'data' field.
        Returns a DataModelException properly populated.
        """
        name = getattr(error, "name", error_codes.UNKNOWN)
        description = error_codes.get_desc(name)
        if str(error):
            description += "::" + str(error)
        return cls(error_code=error_codes.get_num(name),
                   description=description,
                   data=getattr(error, 'data', None),
                   http_status_code=error_codes.get_http(name))
Example #4
0
 def test_add_item_with_long_abbreviation(self):
     test = {
         "name": "test",
         "description": "test description",
         "kind": "nonrecursive",
         "abbreviation": "123456"
     }
     try:
         self.policy_model.add_item(test, {})
         self.fail("DataModelException should been raised.")
     except webservice.DataModelException as e:
         error_key = 'policy_abbreviation_error'
         self.assertEqual(error_codes.get_num(error_key), e.error_code)
         self.assertEqual(error_codes.get_desc(error_key), e.description)
         self.assertEqual(error_codes.get_http(error_key),
                          e.http_status_code)
Example #5
0
 def test_add_item_with_long_abbreviation(self):
     test = {
         "name": "test",
         "description": "test description",
         "kind": "nonrecursive",
         "abbreviation": "123456"
     }
     try:
         self.policy_model.add_item(test, {})
         self.fail("DataModelException should been raised.")
     except webservice.DataModelException as e:
         error_key = 'policy_abbreviation_error'
         self.assertEqual(error_codes.get_num(error_key), e.error_code)
         self.assertEqual(error_codes.get_desc(error_key), e.description)
         self.assertEqual(error_codes.get_http(error_key),
                          e.http_status_code)
Example #6
0
    def __init__(self, message=None, **kwargs):
        """FIXME(thinrichs):

        We just want name and data as fields.
        :param name will be a name from error_codes, which includes the basic
        message.
        :param data will contain specifics for this instance of the
        exception, e.g. a description error message.
        """
        self.data = kwargs.get('data', None)
        self.name = kwargs.get('name', None)

        # TODO(thinrichs): remove the rest of this (except the call to super)
        self.kwargs = kwargs
        if 'code' not in self.kwargs:
            try:
                self.kwargs['code'] = self.code
            except AttributeError:
                pass

        if not message:
            if self.name is not None:
                error_code = error_codes.get_num(self.name)
                description = error_codes.get_desc(self.name)
                message = "(%s) %s" % (error_code, description)
            else:
                try:
                    message = self.msg_fmt % kwargs

                except Exception:
                    exc_info = sys.exc_info()
                    # kwargs doesn't match a variable in the message
                    # log the issue and the kwargs
                    LOG.exception(_('Exception in string format operation'))
                    for name, value in kwargs.items():
                        LOG.error("%s: %s", name, value)    # noqa

                    if CONF.fatal_exception_format_errors:
                        six.reraise(exc_info[0], exc_info[1], exc_info[2])
                    else:
                        # at least get the core message out
                        message = self.msg_fmt

        super(CongressException, self).__init__(message)
Example #7
0
    def __init__(self, message=None, **kwargs):
        """FIXME(thinrichs):

        We just want name and data as fields.
        :param: name will be a name from error_codes, which includes the basic
        message.
        :param: data will contain specifics for this instance of the
        exception, e.g. a description error message.
        """
        self.data = kwargs.get('data', None)
        self.name = kwargs.get('name', None)

        # TODO(thinrichs): remove the rest of this (except the call to super)
        self.kwargs = kwargs
        if 'code' not in self.kwargs:
            try:
                self.kwargs['code'] = self.code
            except AttributeError:
                pass

        if not message:
            if self.name is not None:
                error_code = error_codes.get_num(self.name)
                description = error_codes.get_desc(self.name)
                message = "(%s) %s" % (error_code, description)
            else:
                try:
                    message = self.msg_fmt % kwargs

                except Exception:
                    exc_info = sys.exc_info()
                    # kwargs doesn't match a variable in the message
                    # log the issue and the kwargs
                    LOG.exception(_('Exception in string format operation'))
                    for name, value in kwargs.items():
                        LOG.error("%s: %s", name, value)  # noqa

                    if CONF.fatal_exception_format_errors:
                        six.reraise(exc_info[0], exc_info[1], exc_info[2])
                    else:
                        # at least get the core message out
                        message = self.msg_fmt
        super(CongressException, self).__init__(message)
Example #8
0
    def test_get_error_code(self):
        name = 'fake-error'
        error_codes.errors = {
            "fake-error": (0000, 'This is a fake error code.', 400)
        }
        expected_num = 0000
        expected_desc = 'This is a fake error code.'
        expected_http = 400
        expected_ret = (expected_num, expected_desc)

        ret = error_codes.get(name)
        self.assertEqual(expected_ret, ret)

        num = error_codes.get_num(name)
        self.assertEqual(expected_num, num)

        desc = error_codes.get_desc(name)
        self.assertEqual(expected_desc, desc)

        http = error_codes.get_http(name)
        self.assertEqual(expected_http, http)
Example #9
0
    def test_get_error_code(self):
        name = 'fake-error'
        error_codes.errors = {
            "fake-error": (0000, 'This is a fake error code.', 400)
        }
        expected_num = 0000
        expected_desc = 'This is a fake error code.'
        expected_http = 400
        expected_ret = (expected_num, expected_desc)

        ret = error_codes.get(name)
        self.assertEqual(expected_ret, ret)

        num = error_codes.get_num(name)
        self.assertEqual(expected_num, num)

        desc = error_codes.get_desc(name)
        self.assertEqual(expected_desc, desc)

        http = error_codes.get_http(name)
        self.assertEqual(expected_http, http)
Example #10
0
    def test_get_unknown__error_code(self):
        name = 'fake_error_code'
        error_codes.errors = {
            error_codes.UNKNOWN: (0000, 'Unknown error', 400),
            'fake-error': (1000, 'Fake error', 404)
        }
        expected_num = 0000
        expected_desc = 'Unknown error'
        expected_http = 400
        expected_ret = (expected_num, expected_desc)

        ret = error_codes.get(name)
        self.assertEqual(expected_ret, ret)

        num = error_codes.get_num(name)
        self.assertEqual(expected_num, num)

        desc = error_codes.get_desc(name)
        self.assertEqual(expected_desc, desc)

        http = error_codes.get_http(name)
        self.assertEqual(expected_http, http)
Example #11
0
    def test_get_unknown__error_code(self):
        name = 'fake_error_code'
        error_codes.errors = {
            error_codes.UNKNOWN: (0000, 'Unknown error', 400),
            'fake-error': (1000, 'Fake error', 404)
        }
        expected_num = 0000
        expected_desc = 'Unknown error'
        expected_http = 400
        expected_ret = (expected_num, expected_desc)

        ret = error_codes.get(name)
        self.assertEqual(expected_ret, ret)

        num = error_codes.get_num(name)
        self.assertEqual(expected_num, num)

        desc = error_codes.get_desc(name)
        self.assertEqual(expected_desc, desc)

        http = error_codes.get_http(name)
        self.assertEqual(expected_http, http)