Cannot Read Urllib Error Message Once It Is Read()
My problem is with error handling of the python urllib error object. I am unable to read the error message while still keeping it intact in the error object, for it to be consumed
Solution 1:
This is what I did. Worked for me.
When reading the error for the first time, save it to a variable like this: msg = response.read().decode('utf8')
. You can then create a new HTTPError
instance, with the message, and propagate it.
resp = urllib.request.urlopen(request)
msg = resp.read().decode('utf8')
self.log.exception(msg)
raise HTTPError(resp.url, resp.code, resp.reason, resp.headers, io.BytesIO(bytes(msg, 'utf8')))
Solution 2:
The error object may read from the network. Network is not seekable -- you can't go back in the general case.
You could replace err
with a new HTTPError instance that reads from a buffer (like io.BytesIO()
) instead of the network e.g., (not tested):
content = err.read()
self.log.exception(content)
raise HTTPError(err.url, err.code, err.reason, err.headers, io.BytesIO(content))
Though I'm not sure that you should -- handle the error in a single place instead e.g., reraise a more application specific exception or leave the logging to an upstream handler.
Post a Comment for "Cannot Read Urllib Error Message Once It Is Read()"