Skip to content
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

Open
8 tasks done
sumit-158 opened this issue Sep 12, 2022 · 5 comments
Open
8 tasks done

How to handle exceptions ? #40

sumit-158 opened this issue Sep 12, 2022 · 5 comments
Labels
question Further information is requested

Comments

@sumit-158
Copy link

sumit-158 commented Sep 12, 2022

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the Asyncer documentation, with the integrated search.
  • I already searched in Google "How to X in Asyncer" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to Asyncer but to AnyIO.
  • I already checked if it is not related to Asyncer but to Trio.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

import asyncer
import logging
from typing import Union

from fastapi import FastAPI

async def func1(name: str):
    return {"message": f"Hello {name} form func1"}


async def func2():
    return {"message": f"Hello {name} form func2"}


@app.get("/test")
async def knowledge_panel(name_str: str):
    async with asyncer.create_task_group() as task_group:
        try:
            soon_value1 = task_group.soonify(func1)(name=name_str)
        except Exception:
            logging.exception()
        try:
            soon_value2 = task_group.soonify(func2)(name=name_str)
        except Exception:
            logging.exception()
    data = [soon_value1.value, soon_value2.value]
    return data

Description

  • How to handle an exception like here soon_value2 will throw an error, then how could I catch the exceptions and return user only single data rather than crashing the whole program?

Operating System

Windows

Operating System Details

No response

asyncer Version

0.0.1

Python Version

Python 3.10.5

Additional Context

No response

@sumit-158 sumit-158 added the question Further information is requested label Sep 12, 2022
@tiangolo
Copy link
Member

I assume you solved it, thanks for closing the issue 👍

@sumit-158
Copy link
Author

@tiangolo not actually, I tried something else but it's not that robust and clean.

@sumit-158 sumit-158 reopened this Jan 13, 2023
@gitierrez
Copy link

@sumit-158 how did you handle this? I'm having the exact same problem and can't find anything about it.

@sumit-158
Copy link
Author

sumit-158 commented Feb 10, 2023

@MarkParker5
Copy link
Contributor

I see there 3 options:

  1. Handle exceptions inside func1 (func2)
async def func1(name: str):
    try:
        return {"message": f"Hello {name} form func1"}
    except:
        return {} # fallback value

...
  1. Add a decorator to wrap functions
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
  1. Wrap entire group call
@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants