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: README.md
+51Lines changed: 51 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -125,3 +125,54 @@ with graph.sync_ctx() as ctx:
125
125
```
126
126
127
127
The ParamInfo has the information about name and parameters signature. It's useful if you want to create a dependency that changes based on parameter name, or signature.
128
+
129
+
130
+
## Exception propagation
131
+
132
+
By default if error happens within the context, we send this error to the dependency,
133
+
so you can close it properly. You can disable this functionality by setting `exception_propagation` parameter to `False`.
134
+
135
+
Let's imagine that you want to get a database session from pool and commit after the function is done.
136
+
137
+
138
+
```python
139
+
asyncdefget_session():
140
+
session = sessionmaker()
141
+
142
+
yield session
143
+
144
+
await session.commit()
145
+
146
+
```
147
+
148
+
But what if the error happened when the dependant function was called? In this case you want to rollback, instead of commit.
149
+
To solve this problem, you can just wrap the `yield` statement in `try except` to handle the error.
150
+
151
+
```python
152
+
asyncdefget_session():
153
+
session = sessionmaker()
154
+
155
+
try:
156
+
yield session
157
+
exceptException:
158
+
await session.rollback()
159
+
return
160
+
161
+
await session.commit()
162
+
163
+
```
164
+
165
+
**Also, as a library developer, you can disable exception propagation**. If you do so, then no exception will ever be propagated to dependencies and no such `try except` expression will ever work.
166
+
167
+
168
+
Example of disabled propogation.
169
+
170
+
```python
171
+
172
+
graph = DependencyGraph(target_func)
173
+
174
+
with graph.sync_ctx(exception_propagation=False) as ctx:
0 commit comments