-
-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to handle exceptions ? #40
Comments
I assume you solved it, thanks for closing the issue 👍 |
@tiangolo not actually, I tried something else but it's not that robust and clean. |
@sumit-158 how did you handle this? I'm having the exact same problem and can't find anything about it. |
@gitierrez I used an async "exception_wrapper" decorator. |
I see there 3 options:
async def func1(name: str):
try:
return {"message": f"Hello {name} form func1"}
except:
return {} # fallback value
...
def catch(func):
async def wrapped(*args, **kwargs):
try:
return await func(*args, **kwargs)
except:
return None
return wrapped
# apply decorator
@catch
def func1(...):
...
def func2(...):
...
@app.get("/test")
async def knowledge_panel(name_str: str):
async with asyncer.create_task_group() as task_group:
soon_value1 = task_group.soonify(func1)(name=name_str) # catch applied to all func1
soon_value2 = task_group.soonify(catch(func2))(name=name_str) # or wrap inly exact call
data = [soon_value1.value, soon_value2.value] # don't forget to handle None case
return data
@app.get("/test")
async def knowledge_panel(name_str: str):
try:
async with asyncer.create_task_group() as task_group:
soon_value1 = task_group.soonify(func1)(name=name_str)
soon_value2 = task_group.soonify(func2)(name=name_str)
data = [soon_value1.value, soon_value2.value]
except:
data = []
return data |
First Check
Commit to Help
Example Code
Description
Operating System
Windows
Operating System Details
No response
asyncer Version
0.0.1
Python Version
Python 3.10.5
Additional Context
No response
The text was updated successfully, but these errors were encountered: