When building asynchronous web applications and APIs with the popular Python aiohttp library, properly handling errors is crucial for robustness and reliability. By default, aiohttp will raise exceptions on errors which will crash your application if unhandled. However, for certain non-critical errors, we may want to handle them more gracefully to keep the application running rather than crashing. That's where the errors=ignore parameter can help.
What does errors=ignore do?
Passing
import aiohttp
async with aiohttp.ClientSession(errors='ignore') as session:
async with session.get('http://invalid-host') as resp:
print(resp.status) # Prints 502
Rather than crashing on the DNS lookup failure for an invalid host, a response is still returned with status 502. This allows catching the error and handling it appropriately in your application code.
When should you use it?
However, it's important not to overuse
What errors get ignored?
By default, the following errors will be ignored and returned as a client response:
However other exceptions like those caused by invalid HTTP syntax or application code errors will still raise exceptions.
See the full list of ignored exceptions in the aiohttp documentation.
Customizing ignored errors
You can customize what errors get ignored by passing an
from aiohttp import ClientConnectorCertificateError
async with aiohttp.ClientSession(errors=(ClientConnectorCertificateError,)) as session:
# Ignores only SSL certificate errors
This gives more control over which specific errors to handle gracefully while letting others fail loudly.
Handling ignored errors
When an error is ignored, the response object will contain details about the failure:
resp = await session.get('http://invalid-host')
print(resp.status) # 502
print(resp.content_type) # None
print(resp.content) # empty byte string
print(resp.exception()) # Returns the ConnectorError exception object
You can check the response attributes like status code, headers, and content to identify the failure, log it, and handle it appropriately.
The key is to anticipate and design your application flows to gracefully capture and recover from expected errors by inspecting the response. Robust error handling is essential for building reliable asynchronous services.
In Summary
Handling errors gracefully is essential for any application interacting with unreliable networks.