Skip to content

Commit dc176f0

Browse files
committed
Fixed error in default exception handler when the context lacks the "exception" key
1 parent 8058b7d commit dc176f0

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

docs/versionhistory.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
Version history
22
===============
33

4-
This library adheres to `Semantic Versioning <http://semver.org/>`_.
4+
This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.
55

66
**UNRELEASED**
77

88
- **BACKWARDS INCOMPATIBLE** Switched Sentry reporter to use sentry-sdk instead of raven
99
- Added support for Python 3.10
1010
- Dropped support for Python 3.5 and 3.6
11+
- Fixed error in default exception handler when the context lacks the ``exception`` key
1112

1213
**1.0.0** (2017-11-26)
1314

src/asphalt/exceptions/component.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
def default_exception_handler(
2626
loop: AbstractEventLoop, context: dict[str, Any], *, ctx: Context
2727
) -> None:
28-
report_exception(ctx, context["message"], context["exception"])
28+
if "exception" in context:
29+
report_exception(ctx, context["message"], context["exception"])
2930

3031

3132
class ExceptionReporterComponent(Component):

tests/test_component.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
from asphalt.exceptions.component import ExceptionReporterComponent
1111

1212

13+
class DummyExceptionReporter(ExceptionReporter):
14+
def report_exception(
15+
self, ctx: Context, exception: BaseException, message: str, extra=None
16+
) -> None:
17+
self.reported_exception = exception
18+
19+
1320
@pytest.mark.parametrize(
1421
"install_default_handler", [True, False], ids=["default", "nodefault"]
1522
)
@@ -69,25 +76,25 @@ async def test_default_exception_handler(event_loop):
6976
exception handler.
7077
7178
"""
72-
reported_exception = reported_message = None
7379

7480
async def fail_task():
7581
return 1 / 0
7682

77-
class DummyExceptionReporter(ExceptionReporter):
78-
def report_exception(
79-
self, ctx: Context, exception: BaseException, message: str, extra=None
80-
) -> None:
81-
nonlocal reported_exception, reported_message
82-
reported_exception = exception
83-
reported_message = message
84-
8583
async with Context() as ctx:
8684
component = ExceptionReporterComponent(backend=DummyExceptionReporter)
8785
await component.start(ctx)
86+
reporter = ctx.get_resource(ExceptionReporter)
8887
event_loop.create_task(fail_task())
8988
await sleep(0.1)
9089
gc.collect()
9190

92-
assert isinstance(reported_exception, ZeroDivisionError)
93-
assert reported_message == "Task exception was never retrieved"
91+
assert isinstance(reporter.reported_exception, ZeroDivisionError)
92+
93+
94+
@pytest.mark.asyncio
95+
async def test_default_exception_handler_no_exception(event_loop):
96+
async with Context() as ctx:
97+
component = ExceptionReporterComponent(backend=DummyExceptionReporter)
98+
await component.start(ctx)
99+
handler = event_loop.get_exception_handler()
100+
handler(event_loop, {"message": "dummy"})

0 commit comments

Comments
 (0)