Esempio n. 1
0
    def __init__(self, message=None, **kwargs):
        self.kwargs = kwargs

        if 'code' not in self.kwargs and hasattr(self, 'code'):
            self.kwargs['code'] = self.code

        if message:
            self.message = message

        try:
            self.message = self.message % kwargs
        except KeyError:
            # kwargs doesn't match a variable in the message
            # log the issue and the kwargs
            LOG.exception(
                _LE('Exception in string format operation, '
                    'kwargs: %s') % kwargs)
            try:
                ferr = CONF.fatal_exception_format_errors
            except cfg.NoSuchOptError:
                ferr = CONF.oslo_versionedobjects.fatal_exception_format_errors
            if ferr:
                raise

        super(BigbangException, self).__init__(self.message)
Esempio n. 2
0
    def validate(self, value):
        if value is None:
            return None

        if not isinstance(value, list):
            raise exception.InvalidValue(value=value, type=self.type_name)

        try:
            return [self.type.validate(v) for v in value]
        except Exception:
            LOG.exception(_LE('Failed to validate received value'))
            raise exception.InvalidValue(value=value, type=self.type_name)
Esempio n. 3
0
    def validate(self, value):
        if value is None:
            return None

        if not isinstance(value, self.user_class):
            try:
                value = self.user_class(**value)
            except Exception:
                LOG.exception(_LE('Failed to validate received value'))
                raise exception.InvalidValue(value=value, type=self.type_name)

        return value
Esempio n. 4
0
    def validate(cls, value, default=None):
        if value is None:
            value = default

        if not isinstance(value, bool):
            try:
                value = strutils.bool_from_string(value, strict=True)
            except Exception:
                LOG.exception(_LE('Failed to convert value to bool'))
                raise exception.InvalidValue(value=value, type=cls.type_name)

        return value
Esempio n. 5
0
    def validate(cls, value):
        if value is None:
            return None

        if not isinstance(value, float):
            try:
                value = float(value)
            except Exception:
                LOG.exception(_LE('Failed to convert value to float'))
                raise exception.InvalidValue(value=value, type=cls.type_name)

        return value
Esempio n. 6
0
    def validate(self, value):
        if value is None:
            return None

        if not isinstance(value, dict):
            raise exception.InvalidValue(value=value, type=self.type_name)

        try:
            return {self.key_type.validate(k): self.value_type.validate(v)
                    for k, v in value.items()}
        except Exception:
            LOG.exception(_LE('Failed to validate received value'))
            raise exception.InvalidValue(value=value, type=self.type_name)
        return value
Esempio n. 7
0
 def validate(cls, value, default=None):
     if value is None:
         return
     elif value.isdigit():
         return value
     elif (value.isalnum() and
               value[:-1].isdigit() and value[-1] in VALID_UNITS.keys()):
         return int(value[:-1]) * VALID_UNITS[value[-1]]
     else:
         LOG.exception(_LE('Failed to validate image size'))
         message = _("""
         size must be either integer or string of below format.
         <integer><memory_unit> memory_unit must be 'k','b','m','g'
         in both cases""")
         raise exception.InvalidValue(message=message, value=value,
                                      type=cls.type_name)
Esempio n. 8
0
 def validate(cls, value, default=None):
     if value is None:
         return
     elif value.isdigit() and int(value) >= MIN_MEMORY_SIZE:
         return value
     elif (value.isalnum() and
               value[:-1].isdigit() and value[-1] in VALID_UNITS.keys()):
         if int(value[:-1]) * VALID_UNITS[value[-1]] >= MIN_MEMORY_SIZE:
             return value
     LOG.exception(_LE('Failed to validate container memory value'))
     message = """
     memory must be either integer or string of below format.
     <integer><memory_unit> memory_unit must be 'k','b','m','g'
     in both cases"""
     raise exception.InvalidValue(message=message, value=value,
                                  type=cls.type_name)
Esempio n. 9
0
    def validate(cls, value, minimum=None, maximum=None):
        if value is None:
            return None

        if not isinstance(value, six.integer_types):
            try:
                value = int(value)
            except Exception:
                LOG.exception(_LE('Failed to convert value to int'))
                raise exception.InvalidValue(value=value, type=cls.type_name)

        if minimum is not None and value < minimum:
            message = _("Integer '%(value)s' is smaller than "
                        "'%(min)d'.") % {'value': value, 'min': minimum}
            raise exception.InvalidValue(message=message)

        if maximum is not None and value > maximum:
            message = _("Integer '%(value)s' is large than "
                        "'%(max)d'.") % {'value': value, 'max': maximum}
            raise exception.InvalidValue(message=message)

        return value
Esempio n. 10
0
    def wrapped(*args, **kw):
        try:
            return func(*args, **kw)
        except Exception as excp:
            if isinstance(excp, BigbangException):
                http_error_code = excp.code
            else:
                http_error_code = 500

            if http_error_code >= 500:
                # log the error message with its associated
                # correlation id
                log_correlation_id = uuidutils.generate_uuid()
                LOG.exception(
                    _LE("%(correlation_id)s:%(excp)s") % {
                        'correlation_id': log_correlation_id,
                        'excp': str(excp)
                    })
                # raise a client error with an obfuscated message
                return func_server_error(log_correlation_id, http_error_code)
            else:
                # raise a client error the original message
                LOG.debug(excp)
                return func_client_error(excp, http_error_code)