github.com/jincm/wesharechain@v0.0.0-20210122032815-1537409ce26a/server/block/util/exception.py (about)

     1  #!/usr/bin/python
     2  # -*- coding: utf-8 -*-
     3  
     4  import six
     5  
     6  class ExpressException(Exception):
     7      """Base Cinder Exception
     8      To correctly use this class, inherit from it and define
     9      a 'message' property. That message will get printf'd
    10      with the keyword arguments provided to the constructor.
    11      """
    12      message = "An unknown exception occurred."
    13      code = 500
    14      headers = {}
    15      safe = False
    16      key = ""
    17  
    18      def __init__(self, message=None, **kwargs):
    19          self.kwargs = kwargs
    20          self.kwargs['message'] = message
    21  
    22          if 'code' not in self.kwargs:
    23              try:
    24                  self.kwargs['code'] = self.code
    25              except AttributeError:
    26                  pass
    27  
    28          for k, v in self.kwargs.items():
    29              if isinstance(v, Exception):
    30                  # NOTE(tommylikehu): If this is a cinder exception it will
    31                  # return the msg object, so we won't be preventing
    32                  # translations.
    33                  self.kwargs[k] = six.text_type(v)
    34  
    35          if self._should_format():
    36              try:
    37                  _message = ""
    38                  for k, v in kwargs.items():
    39                      _message += '{k}:{v};'.format(v=v, k=k)
    40                  #message = self.message % kwargs
    41                  message = self.message % {"message": _message}
    42                  message = message.decode('utf-8')
    43  
    44              except Exception:
    45                  # NOTE(melwitt): This is done in a separate method so it can be
    46                  # monkey-patched during testing to make it a hard failure.
    47                  #self._log_exception()
    48                  message = self.message
    49          elif isinstance(message, Exception):
    50              # NOTE(tommylikehu): If this is a cinder exception it will
    51              # return the msg object, so we won't be preventing
    52              # translations.
    53              message = six.text_type(message)
    54  
    55          # NOTE(luisg): We put the actual message in 'msg' so that we can access
    56          # it, because if we try to access the message via 'message' it will be
    57          # overshadowed by the class' message attribute
    58          self.msg = message
    59          super(ExpressException, self).__init__(message)
    60          # Oslo.messaging use the argument 'message' to rebuild exception
    61          # directly at the rpc client side, therefore we should not use it
    62          # in our keyword arguments, otherwise, the rebuild process will fail
    63          # with duplicate keyword exception.
    64          self.kwargs.pop('message', None)
    65  
    66      def _log_exception(self):
    67          pass
    68  
    69      def _should_format(self):
    70          return self.kwargs['message'] is None and '%(message)' in self.message
    71  
    72      # NOTE(tommylikehu): self.msg is already an unicode compatible object
    73      # as the __init__ method ensures of it, and we should not be modifying
    74      # it in any way with str(), unicode(), or six.text_type() as we would
    75      # be preventing translations from happening.
    76      def __unicode__(self):
    77          return self.msg
    78  
    79  class NotFound(ExpressException):
    80      message = u"Resource could not be found. %(message)s"
    81      code = 404
    82      safe = True
    83  
    84  class ParamExist(ExpressException):
    85      message = "Params Exist. %(message)s"
    86      code = 408
    87      safe = True
    88  
    89  class FormalError(ExpressException):
    90      message = "Formal Error. %(message)s"
    91      code = 409
    92      safe = True
    93  
    94  class ParamNone(ExpressException):
    95      message = "Param is none. %(message)s"
    96      code = 410
    97      safe = True