TxTechnician
TxTechnician

Reputation: 308

Python requests errors out and I don't know how to catch the error

This is part of a script that iterates over our current ecommerce products and then checks stock at the distributor. This code is in a for loop, which is why the end of the except statement has "continue". I'm getting the following error intermittently:

TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

I thought I would be able to catch the error and continue the for loop. What am I doing wrong? I was under the impression that except requests.exceptions.RequestException as _error: would catch all errors.

Here is the code:

try:
    print("Line 85")
    Check_BC_API_CALLS()
    print("Line 87")
    r2 = requests.get('https://api.bigcommerce.com/stores/' + _BcStoreHash + '/v3/catalog/products?page=' + str(_CurrentPage) + '&limit=1', headers=_BcHeaders)
    print("Line 90")
except requests.exceptions.RequestException as _error:
    print("BigCommerce request failed: " + _error)
    Check_BC_API_CALLS()
    continue

Upvotes: 0

Views: 1034

Answers (1)

rzlvmp
rzlvmp

Reputation: 9422

Python exception classes hierarchy is:

IOError that is a base class for RequestException have been merged with OSError after python version 3.3:

Changed in version 3.3: EnvironmentError, IOError, WindowsError, socket.error, select.error and mmap.error have been merged into OSError, and the constructor may return a subclass.

So as you can see RequestException is not a parent class of TimeoutError exception and can't be used to catch this type of errors:

import requests

try:
    raise TimeoutError('TIMEOUT ERROR')

except requests.exceptions.RequestException as e:
    # catch RequestException type errors that are specific for request library only
    # do something
    print("RequestExceptions will be caught")
 
except TimeoutError as e:
    # catch TimeoutError type errors that has same level in hierarchy as RequestException errors
    # do something
    print("TimeoutErrors will be caught")

except OSError as e:
    # catch all OSError type errors. Little bit wider than previous exceptions
    # do something
    print("TimeoutErrors or RequestExceptions or any other OSErrors will be caught")

except Exception as e:
    # catch any python errors, because all errors in python are children (sub types) for Exception class. Most wider exception type
    # do something
    print("TimeoutErrors or RequestExceptions or OSErrors or any other python errors will be caught")

Upvotes: 1

Related Questions