1
1
from json import JSONDecodeError , dumps
2
+ from typing import Optional
2
3
3
4
from requests import Response
4
5
from vonage_utils .errors import VonageError
@@ -21,32 +22,32 @@ class HttpRequestError(VonageError):
21
22
22
23
Args:
23
24
response (requests.Response): The HTTP response object.
24
- content_type (str): The response content type.
25
25
26
26
Attributes:
27
27
response (requests.Response): The HTTP response object.
28
28
message (str): The returned error message.
29
29
"""
30
30
31
- def __init__ (self , response : Response , content_type : str ):
31
+ def __init__ (self , response : Response ):
32
32
self .response = response
33
- self .set_error_message ( self .response , content_type )
33
+ self .message = self ._format_error_message ( )
34
34
super ().__init__ (self .message )
35
35
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 } '
45
39
46
40
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
50
51
51
52
52
53
class AuthenticationError (HttpRequestError ):
@@ -56,15 +57,14 @@ class AuthenticationError(HttpRequestError):
56
57
57
58
Args:
58
59
response (requests.Response): The HTTP response object.
59
- content_type (str): The response content type.
60
60
61
61
Attributes (inherited from HttpRequestError parent exception):
62
62
response (requests.Response): The HTTP response object.
63
63
message (str): The returned error message.
64
64
"""
65
65
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 )
68
68
69
69
70
70
class ForbiddenError (HttpRequestError ):
@@ -74,15 +74,14 @@ class ForbiddenError(HttpRequestError):
74
74
75
75
Args:
76
76
response (requests.Response): The HTTP response object.
77
- content_type (str): The response content type.
78
77
79
78
Attributes (inherited from HttpRequestError parent exception):
80
79
response (requests.Response): The HTTP response object.
81
80
message (str): The returned error message.
82
81
"""
83
82
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 )
86
85
87
86
88
87
class NotFoundError (HttpRequestError ):
@@ -92,15 +91,14 @@ class NotFoundError(HttpRequestError):
92
91
93
92
Args:
94
93
response (requests.Response): The HTTP response object.
95
- content_type (str): The response content type.
96
94
97
95
Attributes (inherited from HttpRequestError parent exception):
98
96
response (requests.Response): The HTTP response object.
99
97
message (str): The returned error message.
100
98
"""
101
99
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 )
104
102
105
103
106
104
class RateLimitedError (HttpRequestError ):
@@ -111,15 +109,14 @@ class RateLimitedError(HttpRequestError):
111
109
112
110
Args:
113
111
response (requests.Response): The HTTP response object.
114
- content_type (str): The response content type.
115
112
116
113
Attributes (inherited from HttpRequestError parent exception):
117
114
response (requests.Response): The HTTP response object.
118
115
message (str): The returned error message.
119
116
"""
120
117
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 )
123
120
124
121
125
122
class ServerError (HttpRequestError ):
@@ -130,15 +127,14 @@ class ServerError(HttpRequestError):
130
127
131
128
Args:
132
129
response (requests.Response): The HTTP response object.
133
- content_type (str): The response content type.
134
130
135
131
Attributes (inherited from HttpRequestError parent exception):
136
132
response (requests.Response): The HTTP response object.
137
133
message (str): The returned error message.
138
134
"""
139
135
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 )
142
138
143
139
144
140
class FileStreamingError (VonageError ):
0 commit comments