Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users

Dashboard
Notifications
Mark all as read
Q&A

How do I get the error message out of a requests exception?

+5
−1

I'm trying to log error messages from Requests exceptions. Example:

try:
    make_web_request()
except RequestException as ex:
    logging.error(ex)

Example output:

ERROR : ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))

...okay, that's technically usable, but it's ugly. It looks more like a repr than an error message. The piece of it I actually want is the 'Connection reset by peer'. In this case I've found that I can get it with log.error(ex.args[0].args[-1]), but I don't think I can count on that working for other subtypes of RequestException (or even other instances of the same type).

Is there a Right Way to do this?

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment

Did you try checking __context__? r~~‭ about 2 months ago

2 answers

+2
−0

The documentation for requests.RequestException

requests.exceptions


This module contains the set of Requests' exceptions.
from urllib3.exceptions import HTTPError as BaseHTTPError


class RequestException(IOError):
    """There was an ambiguous exception that occurred while handling your request.
    """

    def __init__(self, *args, **kwargs):
        """Initialize RequestException with `request` and `response` objects."""
        response = kwargs.pop('response', None)
        self.response = response
        self.request = kwargs.pop('request', None)
        if (response is not None and not self.request and
                hasattr(response, 'request')):
            self.request = self.response.request
        super(RequestException, self).__init__(*args, **kwargs)

So a RequestException is a subclass of IOError with special treatment for arguments 'response' and 'request'. What is available there will depend on the code which creates the exception, which the documentation gives no guarantees about.

Consider this code:

import requests

try:
    raise requests.RequestException("Explosion!")
except requests.RequestException as ex:
    print(ex)
    print("")
    print(dir(ex))
    print("")
    print(ex.args)

The output is :

Explosion!

['__cause__', '__class__', '__context__', '__delattr__', '__dict__', 
'__dir__', '__doc__', '__eq__', '__format__', '__ge__', 
'__getattribute__', '__gt__', '__hash__', '__init__', 
'__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', 
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', 
'__setstate__', '__sizeof__', '__str__', '__subclasshook__', 
'__suppress_context__', '__traceback__', '__weakref__', 'args', 
'characters_written', 'errno', 'filename', 'filename2', 'request', 
'response', 'strerror', 'with_traceback']

('Explosion!',)

So I suspect that the message you are getting is one string which was constructed prior to creating the exception and thus is not available in its components.

Let's check the source git clone https://github.com/psf/requests.git

After some grepping I have not being able to find where your error message generates from. But consider this code from requests/utils.py

    try:
        if not pat.match(value):
            raise InvalidHeader("Invalid return character or leading space in header: %s" % name)
    except TypeError:
        raise InvalidHeader("Value for header {%s: %s} must be of type str or "
                            "bytes, not %s" % (name, value, type(value)))

InvalidHeader is a subclass of RequestException. And as we can see the error message is just a single string. As opposed to having name or value in different fields. So it is only reasonable to assume the same happens with your particular RequestException.

Something you should consider is printing ex.__class__, in my example it would just print <class 'requests.exceptions.RequestException'> . But you'll likely get a different value. Let's say it is an HTTPError exception. Then you could change your code to:

try:
    make_web_request()
except HTTPError as ex:
    logging.error("HTTP error")    
except RequestException as ex:
    logging.error(ex)

You could catch all subclasses of RequestException documented at the 1st link of this answer. That way you can print better message and even make your program react properly to different kinds of errors.

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

+1
−0

but I don't think I can count on that working

There really doesn't seem to be a ready-to-use way to get that message (which feels like a good feature-request).

While ex.args[0].args[-1] is indeed not robust, you could make an error-processor that uses isinstance + getattr + ... to handle more cases; something like this:

def exc_to_message(exc: Exception) -> str:
    reason = getattr(exc, 'reason', None)
    if reason:
        if isinstance(reason, bytes):
            reason = reason.decode('utf-8', errors='replace')
        return reason
    if exc.args:
        arg = exc.args[0]
        if isinstance(arg, Exception) and arg is not exc:
            message = exc_to_message(arg)
            if message:
                return message
        # fallback 1
        return str(arg)
    # fallback 2
    return str(exc)

which is more a proof-of-concept than a complete solution; I think a better solution would require changes in urllib3. Just getting a clear "connection refused at {host}:{port}" right now would require regexes.

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

Sign up to answer this question »