Skip to content

Is it possible to catch exceptions in async generator dependencies? #7949

Answered by tiangolo
fgimian asked this question in Questions
Discussion options

You must be logged in to vote

@fgimian you can't raise exceptions from dependencies after the yield. You can raise before the yield and it will be handled by the default exception handler (even before except code in your dependency is reached).

But the context of dependencies is before/around the context of exception handlers. It happens outside of the request handling.

So, you could even return a response, and then have a bunch of background tasks running using the same DB session from the dependency, and they could throw an error, and it would still be caught by the dependency with yield in the the except block. And that would be long after sending the response. Also, long after the last chance to raise an HTTPExcep…

Replies: 6 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by Kludex
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem question-migrate
4 participants
Converted from issue

This discussion was converted from issue #636 on February 28, 2023 11:31.