github.com/phrase/openapi@v0.0.0-20240514140800-49e8a106740e/openapi-generator/templates/python/exceptions.mustache (about) 1 # coding: utf-8 2 3 {{>partial_header}} 4 5 import six 6 7 8 class OpenApiException(Exception): 9 """The base exception class for all OpenAPIExceptions""" 10 11 12 class ApiTypeError(OpenApiException, TypeError): 13 def __init__(self, msg, path_to_item=None, valid_classes=None, 14 key_type=None): 15 """ Raises an exception for TypeErrors 16 17 Args: 18 msg (str): the exception message 19 20 Keyword Args: 21 path_to_item (list): a list of keys an indices to get to the 22 current_item 23 None if unset 24 valid_classes (tuple): the primitive classes that current item 25 should be an instance of 26 None if unset 27 key_type (bool): False if our value is a value in a dict 28 True if it is a key in a dict 29 False if our item is an item in a list 30 None if unset 31 """ 32 self.path_to_item = path_to_item 33 self.valid_classes = valid_classes 34 self.key_type = key_type 35 full_msg = msg 36 if path_to_item: 37 full_msg = "{0} at {1}".format(msg, render_path(path_to_item)) 38 super(ApiTypeError, self).__init__(full_msg) 39 40 41 class ApiValueError(OpenApiException, ValueError): 42 def __init__(self, msg, path_to_item=None): 43 """ 44 Args: 45 msg (str): the exception message 46 47 Keyword Args: 48 path_to_item (list) the path to the exception in the 49 received_data dict. None if unset 50 """ 51 52 self.path_to_item = path_to_item 53 full_msg = msg 54 if path_to_item: 55 full_msg = "{0} at {1}".format(msg, render_path(path_to_item)) 56 super(ApiValueError, self).__init__(full_msg) 57 58 59 class ApiKeyError(OpenApiException, KeyError): 60 def __init__(self, msg, path_to_item=None): 61 """ 62 Args: 63 msg (str): the exception message 64 65 Keyword Args: 66 path_to_item (None/list) the path to the exception in the 67 received_data dict 68 """ 69 self.path_to_item = path_to_item 70 full_msg = msg 71 if path_to_item: 72 full_msg = "{0} at {1}".format(msg, render_path(path_to_item)) 73 super(ApiKeyError, self).__init__(full_msg) 74 75 76 class ApiException(OpenApiException): 77 78 def __init__(self, status=None, reason=None, http_resp=None): 79 if http_resp: 80 self.status = http_resp.status 81 self.reason = http_resp.reason 82 self.body = http_resp.data 83 self.headers = http_resp.getheaders() 84 else: 85 self.status = status 86 self.reason = reason 87 self.body = None 88 self.headers = None 89 90 def __str__(self): 91 """Custom error messages for exception""" 92 error_message = "({0})\n"\ 93 "Reason: {1}\n".format(self.status, self.reason) 94 if self.headers: 95 error_message += "HTTP response headers: {0}\n".format( 96 self.headers) 97 98 if self.body: 99 error_message += "HTTP response body: {0}\n".format(self.body) 100 101 return error_message 102 103 104 def render_path(path_to_item): 105 """Returns a string representation of a path""" 106 result = "" 107 for pth in path_to_item: 108 if isinstance(pth, six.integer_types): 109 result += "[{0}]".format(pth) 110 else: 111 result += "['{0}']".format(pth) 112 return result