Skip to content

Commit 80c7f49

Browse files
authored
Reraise internal errors in tests (#328)
1 parent 1722546 commit 80c7f49

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

logfire/_internal/formatter.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,7 @@ def _fstring_chunks(
202202
for node_value in arg_node.values:
203203
if isinstance(node_value, ast.Constant):
204204
# These are the parts of the f-string not enclosed by `{}`, e.g. 'foo ' in f'foo {bar}'
205-
value = node_value.value
206-
assert type(value) is str
205+
value: str = node_value.value
207206
result.append({'v': value, 't': 'lit'})
208207
new_template += value
209208
else:

logfire/_internal/utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import json
44
import logging
5+
import os
56
import sys
67
from contextlib import contextmanager
78
from pathlib import Path
@@ -226,6 +227,16 @@ def suppress_instrumentation():
226227

227228

228229
def log_internal_error():
230+
try:
231+
# Unless we're specifically testing this function, we should reraise the exception
232+
# in tests for easier debugging.
233+
current_test = os.environ.get('PYTEST_CURRENT_TEST', '')
234+
reraise = bool(current_test and 'test_internal_exception' not in current_test)
235+
except Exception: # pragma: no cover
236+
reraise = False
237+
if reraise:
238+
raise
239+
229240
with suppress_instrumentation(): # prevent infinite recursion from the logging integration
230241
logger.exception('Internal error in Logfire')
231242

tests/test_utils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import requests
33
import requests_mock
44

5-
from logfire._internal.utils import UnexpectedResponse
5+
from logfire._internal.utils import UnexpectedResponse, handle_internal_errors
66

77

88
def test_raise_for_status() -> None:
@@ -15,3 +15,9 @@ def test_raise_for_status() -> None:
1515
s = str(exc_info.value)
1616
assert s.startswith('Unexpected response 503')
1717
assert 'body: this is the body' in s
18+
19+
20+
def test_reraise_internal_exception():
21+
with pytest.raises(ZeroDivisionError):
22+
with handle_internal_errors():
23+
str(1 / 0)

0 commit comments

Comments
 (0)