[Mod] onFailed只接收两个参数,原来的data参数可以从req.response.raw读出
This commit is contained in:
parent
e6f7781a47
commit
6e71c59c06
@ -30,7 +30,7 @@ class TestRestClient(RestClient):
|
|||||||
def onError(self, exceptionType, exceptionValue, tb, req):
|
def onError(self, exceptionType, exceptionValue, tb, req):
|
||||||
self.p.set_exception(exceptionValue)
|
self.p.set_exception(exceptionValue)
|
||||||
|
|
||||||
def onFailed(self, httpStatusCode, data, req):
|
def onFailed(self, httpStatusCode, req):
|
||||||
self.p.set_exception(FailedError("request failed"))
|
self.p.set_exception(FailedError("request failed"))
|
||||||
|
|
||||||
|
|
||||||
@ -71,7 +71,7 @@ class RestfulClientTest(unittest.TestCase):
|
|||||||
def callback(data, req):
|
def callback(data, req):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
self.c.addReq('POST', '/status/201', callback)
|
self.c.addReq('POST', '/status/401', callback)
|
||||||
with self.assertRaises(FailedError):
|
with self.assertRaises(FailedError):
|
||||||
self.c.p.get(3)
|
self.c.p.get(3)
|
||||||
|
|
||||||
|
@ -61,6 +61,11 @@ class Request(object):
|
|||||||
def error(self):
|
def error(self):
|
||||||
return self._status == RequestStatus.error
|
return self._status == RequestStatus.error
|
||||||
|
|
||||||
|
#----------------------------------------------------------------------
|
||||||
|
@property
|
||||||
|
def response(self): # type: ()->requests.Response
|
||||||
|
return self._response
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
statusCode = 'not finished'
|
statusCode = 'not finished'
|
||||||
@ -171,12 +176,10 @@ class RestClient(object):
|
|||||||
return req
|
return req
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def onFailed(self, httpStatusCode, data, req):
|
def onFailed(self, httpStatusCode, req): # type:(int, Request)->None
|
||||||
"""
|
"""
|
||||||
请求失败处理函数(HttpStatusCode!=200).
|
请求失败处理函数(HttpStatusCode!=200).
|
||||||
默认行为是打印到stderr
|
默认行为是打印到stderr
|
||||||
|
|
||||||
@:param data 这个data是原始数据,并不是dict。而且有可能为null。
|
|
||||||
"""
|
"""
|
||||||
print("reuqest : {} {} failed with {}: \n"
|
print("reuqest : {} {} failed with {}: \n"
|
||||||
"headers: {}\n"
|
"headers: {}\n"
|
||||||
@ -188,7 +191,7 @@ class RestClient(object):
|
|||||||
req.headers,
|
req.headers,
|
||||||
req.params,
|
req.params,
|
||||||
req.data,
|
req.data,
|
||||||
data))
|
req._response.raw))
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def onError(self, exceptionType, exceptionValue, tb, req):
|
def onError(self, exceptionType, exceptionValue, tb, req):
|
||||||
@ -222,7 +225,7 @@ class RestClient(object):
|
|||||||
|
|
||||||
# 若没有onFailed或者没设置skipDefaultOnFailed,则调用默认的处理函数
|
# 若没有onFailed或者没设置skipDefaultOnFailed,则调用默认的处理函数
|
||||||
if not req.onFailed or not req.skipDefaultOnFailed:
|
if not req.onFailed or not req.skipDefaultOnFailed:
|
||||||
self.onFailed(httpStatusCode, response.raw, req)
|
self.onFailed(httpStatusCode, req)
|
||||||
except:
|
except:
|
||||||
req._status = RequestStatus.error
|
req._status = RequestStatus.error
|
||||||
t, v, tb = sys.exc_info()
|
t, v, tb = sys.exc_info()
|
||||||
|
Loading…
Reference in New Issue
Block a user