Skip to content

Commit 64af962

Browse files
committed
move fallback handler to wrapper.py
1 parent 38dd4c0 commit 64af962

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

datadog_lambda/handler.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,14 @@
99
import os
1010
import time
1111

12-
from datadog_lambda.tracing import emit_telemetry_on_exception_outside_of_handler
13-
from datadog_lambda.wrapper import datadog_lambda_wrapper
12+
from datadog_lambda.wrapper import datadog_lambda_wrapper, error_fallback_handler
1413
from datadog_lambda.module_name import modify_module_name
1514

1615

1716
class HandlerError(Exception):
1817
pass
1918

2019

21-
class _ErrorFallbackHandler(object):
22-
"""
23-
Decorator for when an exception occurs outside of the handler function.
24-
Emits telemetry and re-raises the exception.
25-
"""
26-
27-
def __init__(self, exception, modified_mod_name, start_time_ns):
28-
self.exception = exception
29-
self.modified_mod_name = modified_mod_name
30-
self.start_time_ns = start_time_ns
31-
32-
def __call__(self, event, context, **kwargs):
33-
emit_telemetry_on_exception_outside_of_handler(
34-
context,
35-
self.exception,
36-
self.modified_mod_name,
37-
self.start_time_ns,
38-
)
39-
raise self.exception
40-
41-
4220
path = os.environ.get("DD_LAMBDA_HANDLER", None)
4321
if path is None:
4422
raise HandlerError(
@@ -58,4 +36,4 @@ def __call__(self, event, context, **kwargs):
5836
handler_func = getattr(handler_module, handler_name)
5937
handler = datadog_lambda_wrapper(handler_func)
6038
except Exception as e:
61-
handler = _ErrorFallbackHandler(e, modified_mod_name, start_time_ns)
39+
handler = error_fallback_handler(e, modified_mod_name, start_time_ns)

datadog_lambda/wrapper.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
is_authorizer_response,
4545
tracer,
4646
propagator,
47+
emit_telemetry_on_exception_outside_of_handler,
4748
)
4849
from datadog_lambda.trigger import (
4950
extract_trigger_tags,
@@ -382,9 +383,31 @@ def _after(self, event, context):
382383
logger.error(format_err_with_traceback(e))
383384

384385

386+
class _ErrorFallbackHandler(object):
387+
"""
388+
Fallback handler for when an exception occurs outside of the handler function.
389+
Emits telemetry and re-raises the exception.
390+
"""
391+
392+
def __init__(self, exception, modified_mod_name, start_time_ns):
393+
self.exception = exception
394+
self.modified_mod_name = modified_mod_name
395+
self.start_time_ns = start_time_ns
396+
397+
def __call__(self, event, context, **kwargs):
398+
emit_telemetry_on_exception_outside_of_handler(
399+
context,
400+
self.exception,
401+
self.modified_mod_name,
402+
self.start_time_ns,
403+
)
404+
raise self.exception
405+
406+
385407
def format_err_with_traceback(e):
386408
tb = traceback.format_exc().replace("\n", "\r")
387409
return f"Error {e}. Traceback: {tb}"
388410

389411

390412
datadog_lambda_wrapper = _LambdaDecorator
413+
error_fallback_handler = _ErrorFallbackHandler

tests/test_handler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def test_dd_lambda_handler_env_var_malformed(self):
3030
)
3131

3232
@patch.dict(os.environ, {"DD_LAMBDA_HANDLER": "nonsense.nonsense"}, clear=True)
33-
@patch("datadog_lambda.tracing.emit_telemetry_on_exception_outside_of_handler")
33+
@patch("datadog_lambda.wrapper.emit_telemetry_on_exception_outside_of_handler")
3434
@patch("time.time_ns", return_value=42)
3535
def test_exception_importing_module(self, mock_time, mock_emit_telemetry):
3636
with self.assertRaises(ModuleNotFoundError) as test_context:
@@ -44,7 +44,7 @@ def test_exception_importing_module(self, mock_time, mock_emit_telemetry):
4444

4545
@patch.dict(os.environ, {"DD_LAMBDA_HANDLER": "nonsense.nonsense"}, clear=True)
4646
@patch("importlib.import_module", return_value=None)
47-
@patch("datadog_lambda.tracing.emit_telemetry_on_exception_outside_of_handler")
47+
@patch("datadog_lambda.wrapper.emit_telemetry_on_exception_outside_of_handler")
4848
@patch("time.time_ns", return_value=42)
4949
def test_exception_getting_handler_func(
5050
self, mock_time, mock_emit_telemetry, mock_import
@@ -60,7 +60,7 @@ def test_exception_getting_handler_func(
6060

6161
@patch.dict(os.environ, {"DD_LAMBDA_HANDLER": "nonsense.nonsense"}, clear=True)
6262
@patch("importlib.import_module")
63-
@patch("datadog_lambda.tracing.emit_telemetry_on_exception_outside_of_handler")
63+
@patch("datadog_lambda.wrapper.emit_telemetry_on_exception_outside_of_handler")
6464
@patch("time.time_ns", return_value=42)
6565
@patch("datadog_lambda.wrapper.datadog_lambda_wrapper")
6666
def test_handler_success(

0 commit comments

Comments
 (0)