forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_exceptiongroup.py
More file actions
476 lines (415 loc) · 13.8 KB
/
Copy pathtest_exceptiongroup.py
File metadata and controls
476 lines (415 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
import sys
import pytest
from sentry_sdk.utils import event_from_exception
try:
# Python 3.11
from builtins import ExceptionGroup # type: ignore
except ImportError:
# Python 3.10 and below
ExceptionGroup = None
minimum_python_311 = pytest.mark.skipif(
sys.version_info < (3, 11), reason="ExceptionGroup tests need Python >= 3.11"
)
@minimum_python_311
def test_exceptiongroup():
exception_group = None
try:
try:
raise RuntimeError("something")
except RuntimeError:
raise ExceptionGroup(
"nested",
[
ValueError(654),
ExceptionGroup(
"imports",
[
ImportError("no_such_module"),
ModuleNotFoundError("another_module"),
],
),
TypeError("int"),
],
)
except ExceptionGroup as e:
exception_group = e
(event, _) = event_from_exception(
exception_group,
client_options={
"include_local_variables": True,
"include_source_context": True,
"max_value_length": 1024,
},
mechanism={"type": "test_suite", "handled": False},
)
values = event["exception"]["values"]
# For this test the stacktrace and the module is not important
for x in values:
if "stacktrace" in x:
del x["stacktrace"]
if "module" in x:
del x["module"]
expected_values = [
{
"mechanism": {
"exception_id": 6,
"handled": False,
"parent_id": 0,
"source": "exceptions[2]",
"type": "chained",
},
"type": "TypeError",
"value": "int",
},
{
"mechanism": {
"exception_id": 5,
"handled": False,
"parent_id": 3,
"source": "exceptions[1]",
"type": "chained",
},
"type": "ModuleNotFoundError",
"value": "another_module",
},
{
"mechanism": {
"exception_id": 4,
"handled": False,
"parent_id": 3,
"source": "exceptions[0]",
"type": "chained",
},
"type": "ImportError",
"value": "no_such_module",
},
{
"mechanism": {
"exception_id": 3,
"handled": False,
"is_exception_group": True,
"parent_id": 0,
"source": "exceptions[1]",
"type": "chained",
},
"type": "ExceptionGroup",
"value": "imports",
},
{
"mechanism": {
"exception_id": 2,
"handled": False,
"parent_id": 0,
"source": "exceptions[0]",
"type": "chained",
},
"type": "ValueError",
"value": "654",
},
{
"mechanism": {
"exception_id": 1,
"handled": False,
"parent_id": 0,
"source": "__context__",
"type": "chained",
},
"type": "RuntimeError",
"value": "something",
},
{
"mechanism": {
"exception_id": 0,
"handled": False,
"is_exception_group": True,
"type": "test_suite",
},
"type": "ExceptionGroup",
"value": "nested",
},
]
assert values == expected_values
@minimum_python_311
def test_exceptiongroup_simple():
exception_group = None
try:
raise ExceptionGroup(
"simple",
[
RuntimeError("something strange's going on"),
],
)
except ExceptionGroup as e:
exception_group = e
(event, _) = event_from_exception(
exception_group,
client_options={
"include_local_variables": True,
"include_source_context": True,
"max_value_length": 1024,
},
mechanism={"type": "test_suite", "handled": False},
)
exception_values = event["exception"]["values"]
assert len(exception_values) == 2
assert exception_values[0]["type"] == "RuntimeError"
assert exception_values[0]["value"] == "something strange's going on"
assert exception_values[0]["mechanism"] == {
"type": "chained",
"handled": False,
"exception_id": 1,
"source": "exceptions[0]",
"parent_id": 0,
}
assert exception_values[1]["type"] == "ExceptionGroup"
assert exception_values[1]["value"] == "simple"
assert exception_values[1]["mechanism"] == {
"type": "test_suite",
"handled": False,
"exception_id": 0,
"is_exception_group": True,
}
frame = exception_values[1]["stacktrace"]["frames"][0]
assert frame["module"] == "tests.test_exceptiongroup"
assert frame["context_line"] == " raise ExceptionGroup("
@minimum_python_311
def test_exception_chain_cause():
exception_chain_cause = ValueError("Exception with cause")
exception_chain_cause.__context__ = TypeError("Exception in __context__")
exception_chain_cause.__cause__ = TypeError(
"Exception in __cause__"
) # this implicitly sets exception_chain_cause.__suppress_context__=True
(event, _) = event_from_exception(
exception_chain_cause,
client_options={
"include_local_variables": True,
"include_source_context": True,
"max_value_length": 1024,
},
mechanism={"type": "test_suite", "handled": False},
)
expected_exception_values = [
{
"mechanism": {
"handled": False,
"type": "test_suite",
},
"module": None,
"type": "TypeError",
"value": "Exception in __cause__",
},
{
"mechanism": {
"handled": False,
"type": "test_suite",
},
"module": None,
"type": "ValueError",
"value": "Exception with cause",
},
]
exception_values = event["exception"]["values"]
assert exception_values == expected_exception_values
@minimum_python_311
def test_exception_chain_context():
exception_chain_context = ValueError("Exception with context")
exception_chain_context.__context__ = TypeError("Exception in __context__")
(event, _) = event_from_exception(
exception_chain_context,
client_options={
"include_local_variables": True,
"include_source_context": True,
"max_value_length": 1024,
},
mechanism={"type": "test_suite", "handled": False},
)
expected_exception_values = [
{
"mechanism": {
"handled": False,
"type": "test_suite",
},
"module": None,
"type": "TypeError",
"value": "Exception in __context__",
},
{
"mechanism": {
"handled": False,
"type": "test_suite",
},
"module": None,
"type": "ValueError",
"value": "Exception with context",
},
]
exception_values = event["exception"]["values"]
assert exception_values == expected_exception_values
@minimum_python_311
def test_simple_exception():
simple_excpetion = ValueError("A simple exception")
(event, _) = event_from_exception(
simple_excpetion,
client_options={
"include_local_variables": True,
"include_source_context": True,
"max_value_length": 1024,
},
mechanism={"type": "test_suite", "handled": False},
)
expected_exception_values = [
{
"mechanism": {
"handled": False,
"type": "test_suite",
},
"module": None,
"type": "ValueError",
"value": "A simple exception",
},
]
exception_values = event["exception"]["values"]
assert exception_values == expected_exception_values
@minimum_python_311
def test_exceptiongroup_starlette_collapse():
"""
Simulates the Starlette collapse_excgroups() pattern where a single-exception
ExceptionGroup is caught and the inner exception is unwrapped and re-raised.
See: https://github.com/Kludex/starlette/blob/0e88e92b592bfa11fd92e331869a8d49ba34b541/starlette/_utils.py#L79-L87
When using FastAPI with multiple BaseHTTPMiddleware instances, anyio wraps
exceptions in ExceptionGroups. Starlette's collapse_excgroups() then unwraps
single-exception groups and re-raises the inner exception.
When re-raising the unwrapped exception, Python implicitly sets __context__
on it pointing back to the ExceptionGroup (because the re-raise happens
inside the except block that caught the ExceptionGroup), creating a cycle:
ExceptionGroup -> .exceptions[0] -> ValueError -> __context__ -> ExceptionGroup
Without cycle detection in exceptions_from_error(), this causes infinite
recursion and a silent RecursionError that drops the event.
"""
exception_group = None
try:
try:
raise RuntimeError("something")
except RuntimeError:
raise ExceptionGroup(
"nested",
[
ValueError(654),
],
)
except ExceptionGroup as exc:
exception_group = exc
unwrapped = exc.exceptions[0]
try:
raise unwrapped
except Exception:
pass
(event, _) = event_from_exception(
exception_group,
client_options={
"include_local_variables": True,
"include_source_context": True,
"max_value_length": 1024,
},
mechanism={"type": "test_suite", "handled": False},
)
values = event["exception"]["values"]
# For this test the stacktrace and the module is not important
for x in values:
if "stacktrace" in x:
del x["stacktrace"]
if "module" in x:
del x["module"]
expected_values = [
{
"mechanism": {
"exception_id": 2,
"handled": False,
"parent_id": 0,
"source": "exceptions[0]",
"type": "chained",
},
"type": "ValueError",
"value": "654",
},
{
"mechanism": {
"exception_id": 1,
"handled": False,
"parent_id": 0,
"source": "__context__",
"type": "chained",
},
"type": "RuntimeError",
"value": "something",
},
{
"mechanism": {
"exception_id": 0,
"handled": False,
"is_exception_group": True,
"type": "test_suite",
},
"type": "ExceptionGroup",
"value": "nested",
},
]
assert values == expected_values
@minimum_python_311
def test_cyclic_exception_group_cause():
"""
Test case related to `test_exceptiongroup_starlette_collapse` above. We want to make sure that
the same cyclic loop cannot happen via the __cause__ as well as the __context__
"""
original = ValueError("original error")
group = ExceptionGroup("unhandled errors in a TaskGroup", [original])
original.__cause__ = group
original.__suppress_context__ = True
# When the ExceptionGroup is the top-level exception, exceptions_from_error
# is called directly (not walk_exception_chain which has cycle detection).
(event, _) = event_from_exception(
group,
client_options={
"include_local_variables": True,
"include_source_context": True,
"max_value_length": 1024,
},
mechanism={"type": "test_suite", "handled": False},
)
exception_values = event["exception"]["values"]
# Must produce a finite list of exceptions without hitting RecursionError.
assert len(exception_values) >= 1
exc_types = [v["type"] for v in exception_values]
assert "ExceptionGroup" in exc_types
assert "ValueError" in exc_types
@minimum_python_311
def test_deeply_nested_cyclic_exception_group():
"""
Related to the `test_exceptiongroup_starlette_collapse` test above.
Testing a more complex cycle: ExceptionGroup -> ValueError -> __cause__ ->
ExceptionGroup (nested) -> TypeError -> __cause__ -> original ExceptionGroup
"""
inner_error = TypeError("inner")
outer_error = ValueError("outer")
inner_group = ExceptionGroup("inner group", [inner_error])
outer_group = ExceptionGroup("outer group", [outer_error])
# Create a cycle spanning two ExceptionGroups
outer_error.__cause__ = inner_group
outer_error.__suppress_context__ = True
inner_error.__cause__ = outer_group
inner_error.__suppress_context__ = True
(event, _) = event_from_exception(
outer_group,
client_options={
"include_local_variables": True,
"include_source_context": True,
"max_value_length": 1024,
},
mechanism={"type": "test_suite", "handled": False},
)
exception_values = event["exception"]["values"]
assert len(exception_values) >= 1
exc_types = [v["type"] for v in exception_values]
assert "ExceptionGroup" in exc_types
assert "ValueError" in exc_types
assert "TypeError" in exc_types