Requests Exceptions

1.4.0 · maintenance · verified Sat Apr 11

requestsexceptions is a simple utility library designed to normalize the import paths for exceptions within the popular 'requests' HTTP library. It addresses scenarios where 'requests' might bundle 'urllib3' or be distributed with 'urllib3' unbundled, which can lead to inconsistencies when trying to catch specific exceptions like `urllib3.exceptions.HTTPError`. The library provides a unified way to import these exceptions, regardless of the underlying 'requests' packaging. Its current version is 1.4.0, released in February 2018, indicating a maintenance-focused cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `requestsexceptions` to catch various network and HTTP-related errors when making requests using the 'requests' library. It covers `ConnectionError`, `Timeout`, `HTTPError`, and the general `RequestException`.

import requests
from requestsexceptions import ConnectionError, Timeout, HTTPError, RequestException

def fetch_url(url):
    try:
        response = requests.get(url, timeout=1)
        response.raise_for_status() # Raises HTTPError for bad responses (4xx or 5xx)
        print(f"Successfully fetched {url}, status: {response.status_code}")
    except ConnectionError:
        print(f"Connection error occurred for {url}.")
    except Timeout:
        print(f"Request timed out for {url}.")
    except HTTPError as e:
        print(f"HTTP error for {url}: {e.response.status_code}")
    except RequestException as e:
        print(f"An unexpected Requests error occurred for {url}: {e}")
    except Exception as e:
        print(f"An non-Requests error occurred for {url}: {e}")

fetch_url('https://httpbin.org/status/200')
fetch_url('https://httpbin.org/status/404')
fetch_url('http://nonexistent.domain.xyz')
fetch_url('https://httpbin.org/delay/5')

view raw JSON →