You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en/docs/tutorial/dependencies/dependencies-with-yield.md
+6-4Lines changed: 6 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -99,7 +99,7 @@ You saw that you can use dependencies with `yield` and have `try` blocks that ca
99
99
100
100
It might be tempting to raise an `HTTPException` or similar in the exit code, after the `yield`. But **it won't work**.
101
101
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`).
103
103
104
104
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.
105
105
@@ -138,9 +138,11 @@ participant tasks as Background tasks
138
138
end
139
139
dep ->> operation: Run dependency, e.g. DB session
140
140
opt raise
141
-
operation -->> handler: Raise HTTPException
141
+
operation -->> dep: Raise HTTPException
142
+
dep -->> handler: Auto forward exception
142
143
handler -->> client: HTTP error response
143
144
operation -->> dep: Raise other exception
145
+
dep -->> handler: Auto forward exception
144
146
end
145
147
operation ->> client: Return response to client
146
148
Note over client,operation: Response is already sent, can't change it anymore
@@ -162,9 +164,9 @@ participant tasks as Background tasks
162
164
After one of those responses is sent, no other response can be sent.
163
165
164
166
!!! 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}.
166
168
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.
0 commit comments