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
|
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
|