11from json import JSONDecodeError , dumps
2+ from typing import Optional
23
34from requests import Response
45from vonage_utils .errors import VonageError
@@ -21,32 +22,32 @@ class HttpRequestError(VonageError):
2122
2223 Args:
2324 response (requests.Response): The HTTP response object.
24- content_type (str): The response content type.
2525
2626 Attributes:
2727 response (requests.Response): The HTTP response object.
2828 message (str): The returned error message.
2929 """
3030
31- def __init__ (self , response : Response , content_type : str ):
31+ def __init__ (self , response : Response ):
3232 self .response = response
33- self .set_error_message ( self .response , content_type )
33+ self .message = self ._format_error_message ( )
3434 super ().__init__ (self .message )
3535
36- def set_error_message (self , response : Response , content_type : str ):
37- body = None
38- if content_type == 'application/json' :
39- try :
40- body = dumps (response .json (), indent = 4 )
41- except JSONDecodeError :
42- pass
43- else :
44- body = response .text
36+ def _format_error_message (self ) -> str :
37+ body = self ._get_response_body ()
38+ base_message = f'{ self .response .status_code } response from { self .response .url } '
4539
4640 if body :
47- self .message = f'{ response .status_code } response from { response .url } . Error response body: \n { body } '
48- else :
49- self .message = f'{ response .status_code } response from { response .url } .'
41+ return f'{ base_message } . Error response body: \n { body } '
42+ return base_message
43+
44+ def _get_response_body (self ) -> Optional [str ]:
45+ if not self .response .content :
46+ return None
47+ try :
48+ return dumps (self .response .json (), indent = 4 )
49+ except JSONDecodeError :
50+ return self .response .text
5051
5152
5253class AuthenticationError (HttpRequestError ):
@@ -56,15 +57,14 @@ class AuthenticationError(HttpRequestError):
5657
5758 Args:
5859 response (requests.Response): The HTTP response object.
59- content_type (str): The response content type.
6060
6161 Attributes (inherited from HttpRequestError parent exception):
6262 response (requests.Response): The HTTP response object.
6363 message (str): The returned error message.
6464 """
6565
66- def __init__ (self , response : Response , content_type : str ):
67- super ().__init__ (response , content_type )
66+ def __init__ (self , response : Response ):
67+ super ().__init__ (response )
6868
6969
7070class ForbiddenError (HttpRequestError ):
@@ -74,15 +74,14 @@ class ForbiddenError(HttpRequestError):
7474
7575 Args:
7676 response (requests.Response): The HTTP response object.
77- content_type (str): The response content type.
7877
7978 Attributes (inherited from HttpRequestError parent exception):
8079 response (requests.Response): The HTTP response object.
8180 message (str): The returned error message.
8281 """
8382
84- def __init__ (self , response : Response , content_type : str ):
85- super ().__init__ (response , content_type )
83+ def __init__ (self , response : Response ):
84+ super ().__init__ (response )
8685
8786
8887class NotFoundError (HttpRequestError ):
@@ -92,15 +91,14 @@ class NotFoundError(HttpRequestError):
9291
9392 Args:
9493 response (requests.Response): The HTTP response object.
95- content_type (str): The response content type.
9694
9795 Attributes (inherited from HttpRequestError parent exception):
9896 response (requests.Response): The HTTP response object.
9997 message (str): The returned error message.
10098 """
10199
102- def __init__ (self , response : Response , content_type : str ):
103- super ().__init__ (response , content_type )
100+ def __init__ (self , response : Response ):
101+ super ().__init__ (response )
104102
105103
106104class RateLimitedError (HttpRequestError ):
@@ -111,15 +109,14 @@ class RateLimitedError(HttpRequestError):
111109
112110 Args:
113111 response (requests.Response): The HTTP response object.
114- content_type (str): The response content type.
115112
116113 Attributes (inherited from HttpRequestError parent exception):
117114 response (requests.Response): The HTTP response object.
118115 message (str): The returned error message.
119116 """
120117
121- def __init__ (self , response : Response , content_type : str ):
122- super ().__init__ (response , content_type )
118+ def __init__ (self , response : Response ):
119+ super ().__init__ (response )
123120
124121
125122class ServerError (HttpRequestError ):
@@ -130,15 +127,14 @@ class ServerError(HttpRequestError):
130127
131128 Args:
132129 response (requests.Response): The HTTP response object.
133- content_type (str): The response content type.
134130
135131 Attributes (inherited from HttpRequestError parent exception):
136132 response (requests.Response): The HTTP response object.
137133 message (str): The returned error message.
138134 """
139135
140- def __init__ (self , response : Response , content_type : str ):
141- super ().__init__ (response , content_type )
136+ def __init__ (self , response : Response ):
137+ super ().__init__ (response )
142138
143139
144140class FileStreamingError (VonageError ):
0 commit comments