Skip to content

Commit 970404d

Browse files
authored
Fix Exception.capture error handling (#600)
Ref #599 `sys.exc_info()` returns `(None, None, None)` if there is no active exception. Still fail hard and fast, but more clearly.
1 parent e15bc80 commit 970404d

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

elasticapm/events.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ def capture(client, exc_info=None, **kwargs):
8787
new_exc_info = True
8888
exc_info = sys.exc_info()
8989

90-
if not exc_info:
91-
raise ValueError("No exception found")
90+
if exc_info == (None, None, None):
91+
raise ValueError("No exception found: capture_exception requires an active exception.")
9292

9393
try:
9494
exc_type, exc_value, exc_traceback = exc_info

tests/events/tests.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@
3232

3333
from __future__ import absolute_import
3434

35+
import pytest
3536
from mock import Mock
3637

37-
from elasticapm.events import Message
38+
from elasticapm.events import Exception, Message
3839

3940

4041
def test_event_to_string():
@@ -46,3 +47,8 @@ def test_event_to_string():
4647
data = {"log": {"message": unformatted_message % (1, 2), "param_message": unformatted_message}}
4748

4849
assert message.to_string(client, data) == formatted_message
50+
51+
52+
def test_capture_exception_no_exception(elasticapm_client):
53+
with pytest.raises(ValueError):
54+
Exception.capture(elasticapm_client)

0 commit comments

Comments
 (0)