8000 📝 Update docs for dependencies with yield · fastapi/fastapi@4531f6a · GitHub
[go: up one dir, main page]

Skip to content

Commit 4531f6a

Browse files
committed
📝 Update docs for dependencies with yield
1 parent c2e3d49 commit 4531f6a

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

docs/en/docs/tutorial/dependencies/dependencies-with-yield.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ You saw that you can use dependencies with `yield` and have `try` blocks that ca
9999

100100
It might be tempting to raise an `HTTPException` or similar in the exit code, after the `yield`. But **it won't work**.
101101

102-
The exit code in dependencies with `yield` is executed *after* [Exception Handlers](../handling-errors.md#install-custom-exception-handlers){.internal-link target=_blank}. There's nothing catching exceptions thrown by your dependencies in the exit code (after the `yield`).
102+
The exit code in dependencies with `yield` is executed *after* the response is sent, so [Exception Handlers](../handling-errors.md#install-custom-exception-handlers){.internal-link target=_blank} will have already run. There's nothing catching exceptions thrown by your dependencies in the exit code (after the `yield`).
103103

104104
So, if you raise an `HTTPException` after the `yield`, the default (or any custom) exception handler that catches `HTTPException`s and returns an HTTP 400 response won't be there to catch that exception anymore.
105105

@@ -138,9 +138,11 @@ participant tasks as Background tasks
138138
end
139139
dep ->> operation: Run dependency, e.g. DB session
140140
opt raise
141-
operation -->> handler: Raise HTTPException
141+
operation -->> dep: Raise HTTPException
142+
dep -->> handler: Auto forward exception
142143
handler -->> client: HTTP error response
143144
operation -->> dep: Raise other exception
145+
dep -->> handler: Auto forward exception
144146
end
145147
operation ->> client: Return response to client
146148
Note over client,operation: Response is already sent, can't change it anymore
@@ -162,9 +164,9 @@ participant tasks as Background tasks
162164
After one of those responses is sent, no other response can be sent.
163165

164166
!!! tip
165-
This diagram shows `HTTPException`, but you could also raise any other exception for which you create a [Custom Exception Handler](../handling-errors.md#install-custom-exception-handlers){.internal-link target=_blank}. And that exception would be handled by that custom exception handler instead of the dependency exit code.
167+
This diagram shows `HTTPException`, but you could also raise any other exception for which you create a [Custom Exception Handler](../handling-errors.md#install-custom-exception-handlers){.internal-link target=_blank}.
166168

167-
But if you raise an exception that is not handled by the exception handlers, it will be handled by the exit code of the dependency.
169+
If you raise any exception, it will be passed to the dependencies with yield, including `HTTPException`, and then **again** to the exception handlers. If there's no exception handler for that exception, it will then be handled by the default internal `ServerErrorMiddleware`, returning a 500 HTTP status code, to let the client know that there was an error in the server.
168170

169171
## Context Managers
170172

0 commit comments

Comments
 (0)
0