11import base64
2+ from socketdev .log import log
3+
24import requests
35from socketdev .core .classes import Response
46from socketdev .exceptions import (
@@ -68,17 +70,21 @@ def format_headers(headers_dict):
6870 try :
6971 error_message = response .json ().get ("error" , {}).get ("message" , "" )
7072 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 ()
7275 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 ()
7478 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 ()
7681 else :
7782 raise APIAccessDenied (f"{ error_message or 'Access denied' } { path_str } { headers_str } " )
7883 except ValueError :
7984 raise APIAccessDenied (f"Access denied{ path_str } { headers_str } " )
8085 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 ()
8288 if response .status_code == 429 :
8389 retry_after = response .headers .get ("retry-after" )
8490 if retry_after :
@@ -91,23 +97,28 @@ def format_headers(headers_dict):
9197 time_msg = f" Retry after: { retry_after } "
9298 else :
9399 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 ()
95102 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 ()
97105 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 } "
101108 )
109+ log .error (error )
110+ raise APIFailure ()
102111
103112 return response
104113
105114 except Timeout :
106115 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 ()
108118 except ConnectionError as error :
109119 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 ()
111122 except (
112123 APIAccessDenied ,
113124 APIInsufficientQuota ,
@@ -123,4 +134,5 @@ def format_headers(headers_dict):
123134 raise
124135 except Exception as error :
125136 # 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