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

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