8

I am creating a download service using the python requests library (See here) to download data from another server. The problem is that sometimes I get a 503 error and I need to display an appropriate message. See sample code below:

import requests
s = requests.Session()
response = s.get('http://mycustomserver.org/download')

I can check from response.status_code and get the status code = 200. But how do I try/catch for a specific error, in this case, I want to be able to detect 503 error and handle them appropriately.

How do I do that?

Frankline
  • 40,277
  • 8
  • 44
  • 75
  • Look at the code: https://raw.githubusercontent.com/kennethreitz/requests/master/requests/exceptions.py, there are many http error codes, there is no exception class for each, instead there is one for all http errors – Christophe Roussy Jun 27 '16 at 08:38

2 Answers2

14

Why not do

class MyException(Exception);
   def __init__(self, error_code, error_msg):
       self.error_code = error_code
       self.error_msg = error_msg

import requests
s = requests.Session()
response = s.get('http://mycustomserver.org/download')

if response.status_code == 503:
    raise MyException(503, "503 error code")

Edit:

It seems that requests library will also raise an Exception for you using response.raise_for_status()

>>> import requests
>>> requests.get('https://google.com/admin')
<Response [404]>
>>> response = requests.get('https://google.com/admin')
>>> response.raise_for_status()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 638, in raise_for_status
    raise http_error
requests.exceptions.HTTPError: 404 Client Error: Not Found

Edit2:

Wrap you raise_for_status with the following try/except

try:
    if response.status_code == 503:
        response.raise_for_status()
except requests.exceptions.HTTPError as e: 
    if e.response.status_code == 503:
        #handle your 503 specific error
Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
  • I was looking at [Errors and Exceptions](http://docs.python-requests.org/en/latest/user/quickstart/#errors-and-exceptions) part and wondering which will enable me to catch the `503 error`. Thanks though. – Frankline Jun 16 '14 at 05:50
  • I see - added the relevant `try/except` block. – Martin Konecny Jun 16 '14 at 05:56
  • @MartinKonecny - How do you check for all errors instead of listing each one by one. ? i.e. http error, bad url error, ssl certificate error, etc.... is there a general exception handler that cover all errors? Or I am just going about this in the wrong way? – Stryker Feb 01 '18 at 17:30
  • 1
    @Stryker You could can catch all exceptions using `except e:` and then over time put in multiple more specific `catch`es to have a unique behaviour for each situation (if you need that of course) – Martin Konecny Feb 07 '18 at 21:45
4

You might as well do this:

try:
    s = requests.Session()
    response = requests.get('http://mycustomserver.org/download')
    if response.status_code == 503:
        response.raise_for_status()
except requests.exceptions.HTTPError:
    print "oops something unexpected happened!"

response.raise_for_status() raises a requests.exceptions.HTTPError and here we are only calling response.raise_for_status() if the status code is equal to 503

K DawG
  • 13,287
  • 9
  • 35
  • 66