66from deeputil import Dummy , ExpiringCache
77
88from .protocols import PROTOCOLS
9- from .exception import NonKeywordArgumentsError
9+ from .exception import NonKeywordArgumentsError , ResponseError
1010from .api import PROTOCOL_HEADER , REQUEST_ID_HEADER
1111from .utils import get_loggable_params
1212
@@ -46,7 +46,9 @@ class Client:
4646
4747 def __init__ (self , url , version = None , protocol = DEFAULT_PROTOCOL ,
4848 path = None , request = '' , timeout = None , dnscache = None ,
49- headers = None , auth = None , stream = False , log = DUMMY_LOG ):
49+ headers = None , auth = None , stream = False , log = DUMMY_LOG ,
50+ raise_exception = True ):
51+
5052 headers = headers or {}
5153
5254 self ._url = url
@@ -61,6 +63,7 @@ def __init__(self, url, version=None, protocol=DEFAULT_PROTOCOL,
6163 self ._auth = auth
6264 self ._stream = stream
6365 self ._log = log
66+ self ._raise_exception = raise_exception
6467
6568 if not self ._dnscache :
6669 self ._dnscache = DNSCache ()
@@ -70,7 +73,8 @@ def _get_state(self):
7073 protocol = self ._protocol , path = self ._path ,
7174 request = self ._request , timeout = self ._timeout ,
7275 dnscache = self ._dnscache , headers = self ._headers ,
73- auth = self ._auth , stream = self ._stream ,log = self ._log )
76+ auth = self ._auth , stream = self ._stream ,log = self ._log ,
77+ raise_exception = self ._raise_exception )
7478
7579 def _copy (self , ** kwargs ):
7680 _kwargs = self ._get_state ()
@@ -110,33 +114,36 @@ def _make_request(self, url, post_body, headers):
110114 if self ._stream :
111115 proto = PROTOCOLS [self ._protocol ]
112116 res = proto .deserialize_stream (res )
113- res = Client ._extract_stream_response (res )
117+ res = Client ._extract_stream_response (res , self . _raise_exception )
114118 else :
115- res = self ._deserialize_response (res .read (), self ._protocol )
119+ res = self ._deserialize_response (res .read (), self ._protocol ,
120+ self ._raise_exception )
116121
117122 return res
118123
119124 @staticmethod
120- def _deserialize_response (data , protocol ):
125+ def _deserialize_response (data , protocol , raise_exception = True ):
121126 proto = PROTOCOLS [protocol ]
122127 r = proto .deserialize (data )
123- return Client ._extract_response (r )
128+ return Client ._extract_response (r , raise_exception )
124129
125130 @staticmethod
126- def _extract_response (r ):
131+ def _extract_response (r , raise_exception = True ):
127132 success = r ['success' ]
128133 if not success :
129134 r .pop ('success' )
130- raise Exception (r ) # FIXME: raise proper exc
135+ r = ResponseError (r )
136+ if raise_exception :
137+ raise r
131138 else :
132139 r = r ['result' ]
133140
134141 return r
135142
136143 @staticmethod
137- def _extract_stream_response (res ):
144+ def _extract_stream_response (res , raise_exception = True ):
138145 for r in res :
139- yield Client ._extract_response (r )
146+ yield Client ._extract_response (r , raise_exception )
140147
141148 @staticmethod
142149 def _serialize_params (params , protocol ):
0 commit comments