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
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

71%
+3 −0
Q&A How do I get the error message out of a requests exception?

The documentation for requests.RequestException requests.exceptions This module contains the set of Requests' exceptions. from urllib3.exceptions import HTTPError as BaseHTTPError class...

posted 3y ago by Estela‭  ·  edited 3y ago by Estela‭

Answer
#2: Post edited by user avatar Estela‭ · 2021-05-13T14:53:50Z (almost 3 years ago)
  • The documentation for [requests.RequestException](https://docs.python-requests.org/en/master/_modules/requests/exceptions/)
  • > 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:
  • ``` Python
  • 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 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.
  • The documentation for [requests.RequestException](https://docs.python-requests.org/en/master/_modules/requests/exceptions/)
  • > 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:
  • ``` Python
  • 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.
#1: Initial revision by user avatar Estela‭ · 2021-05-13T14:52:58Z (almost 3 years ago)
The documentation for [requests.RequestException](https://docs.python-requests.org/en/master/_modules/requests/exceptions/)



 > 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:
``` Python
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 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.