Ciao a tutti! Sto sviluppando un'app async Python 3.11.8 che usa asyncio per chiamate API concorrenti, ma ho problemi a intercettare eccezioni specifiche in coroutines annidate. Ecco un esempio ridotto del problema:
```python
async def fetch_data(url):
try:
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
if response.status == 500:
raise ValueError("Errore server")
return await response.text()
except aiohttp.ClientError as e:
print(f"Errore client: {e}")
async def wrapper():
try:
result = await fetch_data("http://example.com")
print(result)
except ValueError as e:
print(f"Errore personalizzato: {e}")
```
L'errore ValueError non viene mai intercettato nel wrapper, nonostante l'await. Ho provato a spostare try-except dentro fetch_data, ma vorrei gestire errori diversi in livelli diversi. Sto usando aiohttp 3.8.5 e ho controllato la documentazione ufficiale senza trovare soluzioni. Qualcuno ha esperienza con catene di try-except in ambito async? Consigli su dove posizionare i blocchi o alternative migliore?
```python
async def fetch_data(url):
try:
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
if response.status == 500:
raise ValueError("Errore server")
return await response.text()
except aiohttp.ClientError as e:
print(f"Errore client: {e}")
async def wrapper():
try:
result = await fetch_data("http://example.com")
print(result)
except ValueError as e:
print(f"Errore personalizzato: {e}")
```
L'errore ValueError non viene mai intercettato nel wrapper, nonostante l'await. Ho provato a spostare try-except dentro fetch_data, ma vorrei gestire errori diversi in livelli diversi. Sto usando aiohttp 3.8.5 e ho controllato la documentazione ufficiale senza trovare soluzioni. Qualcuno ha esperienza con catene di try-except in ambito async? Consigli su dove posizionare i blocchi o alternative migliore?