Closed edam closed 6 years ago
Exceptions that occur with AsyncRequests are caught and stored in an exception
attribute.
>>> headers = {'foo': 42}
>>> req = grequests.get('https://httpbin.org', headers=headers)
>>> req.send()
<grequests.AsyncRequest object at 0x000001E23EC567F0>
>>> req.response
>>> req.exception # None
InvalidHeader("Value for header {foo: 42} must be of type str or bytes, not <class 'int'>",)
If you're using grequests.map
to send the requests, you should use the exception_handler
argument to deal with errors.
>>> def exception_handler(req, ex):
... print(ex)
... return locals()
...
>>> grequests.map([grequests.get('https://httpbin.org', headers=headers)], exception_handler=exception_handler)
Value for header {foo: 42} must be of type str or bytes, not <class 'int'>
[{'ex': InvalidHeader("Value for header {foo: 42} must be of type str or bytes, not <class 'int'>",), 'req': <grequests.AsyncRequest object at 0x000001E23F99A470>}]
Hope that helps.
Passing headers that contain non-string values, e.g.:
Causes the request to fail and, although response is
None
, no exception is thrown and there is no other sign that the request was not made (that I'm aware of).