Comments (8)
The request is for an error message to be added to the exception. However, in many places we use asyncio.timeout(), which doesn't support any parameters. It's not commonplace to include messages in TimeoutError, and given that we might want to replace the other instances with asyncio.timeout() later, I'd suggest we don't commit to this feature.
from aiohttp.
My understanding of this response is that you propose enhancing
asyncio.Timeout
by adding anexception_cls
parameter and possibly**kwargs
in its__init__()
method.
That's probably more flexible that my initial thought, which was just to have a msg
parameter that's passed to TimeoutError.
from aiohttp.
Perhaps #8089 will help a bit.
from aiohttp.
Perhaps #8089 will help a bit.
I haven't read PR content carefully, but it doesn't seem to have been modified here
Line 705 in cdd5c68
from aiohttp.
The request is for an error message to be added to the exception. However, in many places we use asyncio.timeout(), which doesn't support any parameters. It's not commonplace to include messages in TimeoutError, and given that we might want to replace the other instances with asyncio.timeout() later, I'd suggest we don't commit to this feature.
Appreciate your response to this issue. However, I would like to emphasize the importance of providing error types and details in timeout exceptions, and offer a more accurate comparison based on how requests and httpx handle timeouts.
Current Scenario:
aiohttp Example: Currently, when a timeout occurs in aiohttp, it raises a generic asyncio.TimeoutError
without specifying the type of timeout or providing detailed information.
async with aiohttp.ClientSession() as client:
res = await client.get('https://postman-echo.com/delay/3', timeout=1)
Traceback (most recent call last):
...
TimeoutError: (No detailed message provided)
requests Example (Connection Timeout): The requests library does indeed provide a specific exception type ConnectTimeout
along with detailed error information:
res = requests.get('https://postman-echo.com/delay/3', timeout=0.1)
Traceback (most recent call last):
...
requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='postman-echo.com', port=443): Max retries exceeded with url: /delay/3 (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x107fe8d90>, 'Connection to postman-echo.com timed out. (connect timeout=0.1)'))
requests Example (Read Timeout): Similarly, for read timeouts, requests provides a distinct exception type ReadTimeout
accompanied by a descriptive message:
res = requests.get('https://postman-echo.com/delay/3', timeout=1)
Traceback (most recent call last):
...
requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='postman-echo.com', port=443): Max retries exceeded with url: /delay/3 (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x107fe8d90>, 'Connection to postman-echo.com timed out. (connect timeout=0.1)'))
httpx Example (Connection & Read Timeouts): While httpx offers separate exception types ConnectTimeout
and ReadTimeout
, it doesn't include as much detailed network context information directly as requests:
async with httpx.AsyncClient(verify=False) as client:
res = await client.get('https://postman-echo.com/delay/3', timeout=.1)
Traceback (most recent call last):
...
httpx.ConnectTimeout
async with httpx.AsyncClient(verify=False) as client:
res = await client.get('https://postman-echo.com/delay/3', timeout=1)
Traceback (most recent call last):
...
httpx.ReadTimeout
Considering aiohttp's future plans to utilize asyncio.timeout()
, i suggest considering ways to refine aiohttp's timeout exceptions into different types of timeouts with corresponding context details. This would enable developers to quickly identify the root cause of problems, rather than just encountering a general TimeoutError. Such an improvement would maintain compatibility with asyncio while significantly enhancing users' ability to troubleshoot and resolve timeout-related issues efficiently.
Look forward to further discussing.
from aiohttp.
However, I would like to emphasize the importance of providing error types and details in timeout exceptions
Then maybe also make a proposal to cpython? If they add a message argument or similar to asyncio.timeout()
, then we can use that. Otherwise, we'd have to add a bunch of reraises in, which seems a little awkward to me:
try:
with asyncio.timeout(...):
...
except TimeoutError:
raise TimeoutError("...")
from aiohttp.
My understanding of this response is that you propose enhancing asyncio.Timeout
by adding an exception_cls
parameter and possibly **kwargs
in its __init__()
method. This would allow asyncio.timeout()
to raise a custom exception such as exception_cls(**kwargs)
instead of the default TimeoutError
.
# Hypothetical usage after an update to asyncio.timeout()
class CustomTimeoutError(Exception):
def __init__(self, message, **kwargs):
self.url = url
super().__init__(message)
try:
with asyncio.timeout(5.0, exception_cls=CustomTimeoutError, url="https://example.com"):
# Perform asynchronous operations
...
except CustomTimeoutError as e:
print(f"Timed out while accessing {e.url}: {e}")
I wholeheartedly agree that implementing such a feature would be beneficial. It would significantly improve developers' ability to understand and troubleshoot timeout issues by providing contextually rich error messages.
Meanwhile, I acknowledge your concern about having to introduce awkward re-raising logic within aiohttp if this feature is not available. However, even without the direct support from asyncio
, aiohttp
could still internally handle and convert these exceptions by catching asyncio.TimeoutError
instances and re-raising them as aiohttp-specific subclasses enriched with specific error causes and details.
from aiohttp.
I think, we should consider implementing the suggested context augmentation where possible, if the contributor is willing to present a PoC PR. I'm sure it's useful.
That https://github.com/aio-libs/aiohttp/blob/cdd5c6802e669c93f0077448430349b6dadd8580/aiohttp/helpers.py#L720C45-L720C49 place is an example where we force a from None
, while the context is readily available in the local variable exc_val
.
from aiohttp.
Related Issues (20)
- ClientSession cannot reuse connection pool HOT 4
- Using underscore to name a field in a named tuple HOT 2
- Request Pynacl Encryption Middleware HOT 13
- ASGI support HOT 4
- tests/test_pytest_plugin.py::test_aiohttp_plugin fails on Alpine Linux (python 3.11 and python 3.12) HOT 12
- ERROR: aiohttp has an invalid wheel, .dist-info directory 'yarl-1.9.4.dist-info' does not start with 'aiohttp' HOT 2
- Pass max_length parameter to ZLibDecompressor HOT 5
- "Unclosed client session" when initialization fails HOT 2
- Expired cookies not listed in the response cookies HOT 2
- Reserve generic property on app to store app state in a typed fashion HOT 1
- Please update llhttp to 9.2.1 HOT 7
- Using MultipartWriter.append_json breaks in 3.9.4 with AssertionError "assert CONTENT_DISPOSITION in payload.headers" HOT 8
- Exception occurred while requesting https URL using proxy aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host HOT 3
- Documentation mentions async_timeout as dependency HOT 2
- TimeoutError instead of 403 "Forbidden" in case of not corresponding content length HOT 3
- CONTRIB: easily-integrated minimal http server example HOT 1
- aiohttp ^C hangs when psycopg connection pool created HOT 1
- llhttp should be a separate, optional package HOT 2
- Error message not always propagated on 3.9.4 HOT 2
- ValueError: I/O operation on closed file on WSL HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aiohttp.