44from importlib .metadata import version
55
66from .constants import LTS_VERSION
7- from .exceptions import SeamHttpApiError
7+ from .exceptions import (
8+ SeamHttpApiError ,
9+ SeamHttpInvalidInputError ,
10+ SeamHttpUnauthorizedError ,
11+ )
812from .models import AbstractSeamHttpClient
913
1014SDK_HEADERS = {
@@ -30,10 +34,33 @@ def request(self, method, url, *args, **kwargs):
3034 return self ._handle_response (response )
3135
3236 def _handle_response (self , response : requests .Response ):
33- if response .status_code != 200 :
34- raise SeamHttpApiError (response )
37+ if not 200 <= response .status_code < 300 :
38+ self . _handle_error_response (response )
3539
36- if "application/json" in response .headers [ "content-type" ] :
40+ if "application/json" in response .headers . get ( "content-type" , "" ) :
3741 return response .json ()
3842
3943 return response .text
44+
45+ def _handle_error_response (self , response : requests .Response ):
46+ status_code = response .status_code
47+ request_id = response .headers .get ("seam-request-id" )
48+
49+ if status_code == 401 :
50+ raise SeamHttpUnauthorizedError (request_id )
51+
52+ error = response .json ().get ("error" , {})
53+ error_type = error .get ("type" , "unknown_error" )
54+ error_message = error .get ("message" , "Unknown error" )
55+ error_data = error .get ("data" , None )
56+
57+ error_details = {
58+ "type" : error_type ,
59+ "message" : error_message ,
60+ "data" : error_data ,
61+ }
62+
63+ if error_type == "invalid_input" :
64+ raise SeamHttpInvalidInputError (error_details , status_code , request_id )
65+
66+ raise SeamHttpApiError (error_details , status_code , request_id )
0 commit comments