github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/clients/python-legacy/lakefs_client/exceptions.py (about)

     1  """
     2      lakeFS API
     3  
     4      lakeFS HTTP API  # noqa: E501
     5  
     6      The version of the OpenAPI document: 1.0.0
     7      Contact: services@treeverse.io
     8      Generated by: https://openapi-generator.tech
     9  """
    10  
    11  
    12  
    13  class OpenApiException(Exception):
    14      """The base exception class for all OpenAPIExceptions"""
    15  
    16  
    17  class ApiTypeError(OpenApiException, TypeError):
    18      def __init__(self, msg, path_to_item=None, valid_classes=None,
    19                   key_type=None):
    20          """ Raises an exception for TypeErrors
    21  
    22          Args:
    23              msg (str): the exception message
    24  
    25          Keyword Args:
    26              path_to_item (list): a list of keys an indices to get to the
    27                                   current_item
    28                                   None if unset
    29              valid_classes (tuple): the primitive classes that current item
    30                                     should be an instance of
    31                                     None if unset
    32              key_type (bool): False if our value is a value in a dict
    33                               True if it is a key in a dict
    34                               False if our item is an item in a list
    35                               None if unset
    36          """
    37          self.path_to_item = path_to_item
    38          self.valid_classes = valid_classes
    39          self.key_type = key_type
    40          full_msg = msg
    41          if path_to_item:
    42              full_msg = "{0} at {1}".format(msg, render_path(path_to_item))
    43          super(ApiTypeError, self).__init__(full_msg)
    44  
    45  
    46  class ApiValueError(OpenApiException, ValueError):
    47      def __init__(self, msg, path_to_item=None):
    48          """
    49          Args:
    50              msg (str): the exception message
    51  
    52          Keyword Args:
    53              path_to_item (list) the path to the exception in the
    54                  received_data dict. None if unset
    55          """
    56  
    57          self.path_to_item = path_to_item
    58          full_msg = msg
    59          if path_to_item:
    60              full_msg = "{0} at {1}".format(msg, render_path(path_to_item))
    61          super(ApiValueError, self).__init__(full_msg)
    62  
    63  
    64  class ApiAttributeError(OpenApiException, AttributeError):
    65      def __init__(self, msg, path_to_item=None):
    66          """
    67          Raised when an attribute reference or assignment fails.
    68  
    69          Args:
    70              msg (str): the exception message
    71  
    72          Keyword Args:
    73              path_to_item (None/list) the path to the exception in the
    74                  received_data dict
    75          """
    76          self.path_to_item = path_to_item
    77          full_msg = msg
    78          if path_to_item:
    79              full_msg = "{0} at {1}".format(msg, render_path(path_to_item))
    80          super(ApiAttributeError, self).__init__(full_msg)
    81  
    82  
    83  class ApiKeyError(OpenApiException, KeyError):
    84      def __init__(self, msg, path_to_item=None):
    85          """
    86          Args:
    87              msg (str): the exception message
    88  
    89          Keyword Args:
    90              path_to_item (None/list) the path to the exception in the
    91                  received_data dict
    92          """
    93          self.path_to_item = path_to_item
    94          full_msg = msg
    95          if path_to_item:
    96              full_msg = "{0} at {1}".format(msg, render_path(path_to_item))
    97          super(ApiKeyError, self).__init__(full_msg)
    98  
    99  
   100  class ApiException(OpenApiException):
   101  
   102      def __init__(self, status=None, reason=None, http_resp=None):
   103          if http_resp:
   104              self.status = http_resp.status
   105              self.reason = http_resp.reason
   106              self.body = http_resp.data
   107              self.headers = http_resp.getheaders()
   108          else:
   109              self.status = status
   110              self.reason = reason
   111              self.body = None
   112              self.headers = None
   113  
   114      def __str__(self):
   115          """Custom error messages for exception"""
   116          error_message = "({0})\n"\
   117                          "Reason: {1}\n".format(self.status, self.reason)
   118          if self.headers:
   119              error_message += "HTTP response headers: {0}\n".format(
   120                  self.headers)
   121  
   122          if self.body:
   123              error_message += "HTTP response body: {0}\n".format(self.body)
   124  
   125          return error_message
   126  
   127  
   128  class NotFoundException(ApiException):
   129  
   130      def __init__(self, status=None, reason=None, http_resp=None):
   131          super(NotFoundException, self).__init__(status, reason, http_resp)
   132  
   133  
   134  class UnauthorizedException(ApiException):
   135  
   136      def __init__(self, status=None, reason=None, http_resp=None):
   137          super(UnauthorizedException, self).__init__(status, reason, http_resp)
   138  
   139  
   140  class ForbiddenException(ApiException):
   141  
   142      def __init__(self, status=None, reason=None, http_resp=None):
   143          super(ForbiddenException, self).__init__(status, reason, http_resp)
   144  
   145  
   146  class ServiceException(ApiException):
   147  
   148      def __init__(self, status=None, reason=None, http_resp=None):
   149          super(ServiceException, self).__init__(status, reason, http_resp)
   150  
   151  
   152  def render_path(path_to_item):
   153      """Returns a string representation of a path"""
   154      result = ""
   155      for pth in path_to_item:
   156          if isinstance(pth, int):
   157              result += "[{0}]".format(pth)
   158          else:
   159              result += "['{0}']".format(pth)
   160      return result