1
1
import base64
2
+ from socketdev .log import log
3
+
2
4
import requests
3
5
from socketdev .core .classes import Response
4
6
from socketdev .exceptions import (
@@ -68,17 +70,21 @@ def format_headers(headers_dict):
68
70
try :
69
71
error_message = response .json ().get ("error" , {}).get ("message" , "" )
70
72
if "Insufficient permissions for API method" in error_message :
71
- raise APIInsufficientPermissions (f"{ error_message } { path_str } { headers_str } " )
73
+ log .error (f"{ error_message } { path_str } { headers_str } " )
74
+ raise APIInsufficientPermissions ()
72
75
elif "Organization not allowed" in error_message :
73
- raise APIOrganizationNotAllowed (f"{ error_message } { path_str } { headers_str } " )
76
+ log .error (f"{ error_message } { path_str } { headers_str } " )
77
+ raise APIOrganizationNotAllowed ()
74
78
elif "Insufficient max quota" in error_message :
75
- raise APIInsufficientQuota (f"{ error_message } { path_str } { headers_str } " )
79
+ log .error (f"{ error_message } { path_str } { headers_str } " )
80
+ raise APIInsufficientQuota ()
76
81
else :
77
82
raise APIAccessDenied (f"{ error_message or 'Access denied' } { path_str } { headers_str } " )
78
83
except ValueError :
79
84
raise APIAccessDenied (f"Access denied{ path_str } { headers_str } " )
80
85
if response .status_code == 404 :
81
- raise APIResourceNotFound (f"Path not found { path } { path_str } { headers_str } " )
86
+ log .error (f"Path not found { path } { path_str } { headers_str } " )
87
+ raise APIResourceNotFound ()
82
88
if response .status_code == 429 :
83
89
retry_after = response .headers .get ("retry-after" )
84
90
if retry_after :
@@ -91,23 +97,28 @@ def format_headers(headers_dict):
91
97
time_msg = f" Retry after: { retry_after } "
92
98
else :
93
99
time_msg = ""
94
- raise APIInsufficientQuota (f"Insufficient quota for API route.{ time_msg } { path_str } { headers_str } " )
100
+ log .error (f"Insufficient quota for API route.{ time_msg } { path_str } { headers_str } " )
101
+ raise APIInsufficientQuota ()
95
102
if response .status_code == 502 :
96
- raise APIBadGateway (f"Upstream server error{ path_str } { headers_str } " )
103
+ log .error (f"Upstream server error{ path_str } { headers_str } " )
104
+ raise APIBadGateway ()
97
105
if response .status_code >= 400 :
98
- raise APIFailure (
99
- f"Bad Request: HTTP original_status_code:{ response .status_code } { path_str } { headers_str } " ,
100
- status_code = 500 ,
106
+ error = (
107
+ f"Bad Request: HTTP original_status_code:{ response .status_code } { path_str } { headers_str } "
101
108
)
109
+ log .error (error )
110
+ raise APIFailure ()
102
111
103
112
return response
104
113
105
114
except Timeout :
106
115
request_duration = time .time () - start_time
107
- raise APITimeout (f"Request timed out after { request_duration :.2f} seconds" )
116
+ log .error (f"Request timed out after { request_duration :.2f} seconds" )
117
+ raise APITimeout ()
108
118
except ConnectionError as error :
109
119
request_duration = time .time () - start_time
110
- raise APIConnectionError (f"Connection error after { request_duration :.2f} seconds: { error } " )
120
+ log .error (f"Connection error after { request_duration :.2f} seconds: { error } " )
121
+ raise APIConnectionError ()
111
122
except (
112
123
APIAccessDenied ,
113
124
APIInsufficientQuota ,
@@ -123,4 +134,5 @@ def format_headers(headers_dict):
123
134
raise
124
135
except Exception as error :
125
136
# Only truly unexpected errors get wrapped in a generic APIFailure
126
- raise APIFailure (f"Unexpected error: { error } " , status_code = 500 )
137
+ log .error (f"Unexpected error: { error } " )
138
+ raise APIFailure ()
0 commit comments