Error handling is very important in scenarios of network requests and interactions with external systems;
Why error handling is needed
- Ensure program stability; programs without error handling will crash or stop executing directly when encountering exceptions, which is particularly dangerous in production environments.
- Deal with uncontrollable factors; such as network connection quality, server response time, whether the server is online, etc.
- Handle and diagnose problems; by capturing and handling exceptions, you can record error logs and capture error details, which will help with subsequent debugging and troubleshooting.
- Improve user experience; without error handling, users may encounter program crashes or unresponsiveness. With error handling, you can provide users with friendly prompts to explain the problem and suggest the next step. For example, when the network is interrupted, you can tell the user to try again later.
- Ensure data integrity; if the program does not handle errors when certain operations fail, data loss or inconsistency may occur. By performing error handling before and after key operations, data integrity and security can be ensured.
- Deal with API changes; when interacting with external APIs, the API may change, such as interface format, status code, etc.
No error handling vs. error handling
Code without error handling:
import requests
response = requests.get("https://example.com/api/data")
print(response.json())
If the request fails or the API is unavailable, the program will crash directly and throw an exception, and the user will not understand what went wrong.
There is error handling code
import requests
try:
response = requests.get("https://example.com/api/data")
response.raise_for_status()
data = response.json()
print(data)
except requests.exceptions.HTTPError as http_err:
print(f"HTTP error occurred: {http_err}")
except requests.exceptions.ConnectionError as conn_err:
print(f"Connection error occurred: {conn_err}")
except requests.exceptions.Timeout as timeout_err:
print(f"Timeout error occurred: {timeout_err}")
except requests.exceptions.RequestException as req_err:
print(f"An error occurred: {req_err}")
In this example, the program is able to handle different types of errors and give appropriate feedback without crashing.
Common Requests Error List
Exception | Description |
---|---|
RequestException | This is the base class for all requests exceptions; |
InvalidJSONError | The response data is not a valid JSON format, possibly returning HTML or other non-JSON formatted content. |
JSONDecodeError | The response content cannot be parsed as JSON, usually because the returned data format is incorrect or the data is truncated. |
HTTPError | The HTTP request returned an unsuccessful status code (e.g., 4xx or 5xx), indicating that an error occurred on the client or server side. |
ConnectionError | The network connection failed, possibly because the target server is unreachable, DNS resolution error, or network interruption. |
ProxyError | When using a proxy for requests, the proxy server cannot connect or returns an error, causing the request to fail. |
SSLError | The SSL certificate verification failed, possibly due to an invalid, expired, or untrusted certificate. |
Timeout | The request timed out, usually because the server did not respond within the specified time or due to high network latency. |
ConnectTimeout | The connection timed out, usually because it was unable to establish a connection to the target server within the specified time. |
ReadTimeout | The read timed out, usually because the server did not send response data within the specified time. |
URLRequired | The request is missing a valid URL, making it impossible to execute the HTTP request. |
TooManyRedirects | Too many redirects occurred during the request process, exceeding the allowed maximum number of redirects, usually due to circular redirects or incorrect URL configuration. |
MissingSchema | The requested URL is missing the necessary protocol part (e.g., http:// or https://), making it impossible to recognize the URL. |
InvalidSchema | The requested URL uses an unsupported or invalid protocol (e.g., abc://), making it impossible to process the request. |
InvalidURL | The provided URL format is incorrect, containing illegal characters or a structure that does not conform to specifications. |
InvalidHeader | The request headers contain invalid or non-compliant fields, possibly due to incorrect field names or value formats. |
InvalidProxyURL | The provided proxy URL format is incorrect or contains invalid characters, preventing the establishment of a proxy connection. |
ChunkedEncodingError | When the response content is transmitted using chunked encoding, the received chunk data is incomplete or incorrectly formatted, preventing proper parsing. |
ContentDecodingError | An error occurred during the decoding of the response content, usually because the data format does not conform to the specified decoding type, such as gzip or deflate. |
StreamConsumedError | Attempting to read an already consumed response stream, usually because the response object has been read multiple times, while the requests response stream can only be read once. |
RetryError | The maximum retry limit has been exceeded when retrying the request, usually due to persistent network failures or server unavailability. |