Skip to content

Commit a547560

Browse files
RD-4296 - mimic AWS import strategy of the original handler (#175)
* mimic boostrap.py import strategy of the original handler
1 parent cecc8f3 commit a547560

File tree

2 files changed

+69
-20
lines changed

2 files changed

+69
-20
lines changed

src/lumigo_tracer/auto_instrument_handler.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ def parse_handler():
1010
try:
1111
module_name, unit_name = os.environ[ORIGINAL_HANDLER_KEY].rsplit(".", 1)
1212
except KeyError:
13-
raise ValueError(
13+
raise Exception(
1414
"Could not find the original handler. Please contact Lumigo for more information."
15-
)
16-
except ValueError:
17-
raise RuntimeError(
18-
f"Invalid handler format: Bad handler '{os.environ[ORIGINAL_HANDLER_KEY]}': not enough values to unpack (expected 2, got 1)"
19-
)
15+
) from None
16+
except ValueError as e:
17+
raise ValueError(
18+
f"Runtime.MalformedHandlerName: Bad handler '{os.environ[ORIGINAL_HANDLER_KEY]}': {str(e)}"
19+
) from None
2020
importable_name = module_name.replace("/", ".")
2121
return importable_name, unit_name
2222

@@ -26,11 +26,21 @@ def _handler(*args, **kwargs):
2626
handler_module = ""
2727
try:
2828
handler_module, unit_name = parse_handler()
29-
original_handler = getattr(importlib.import_module(handler_module), unit_name)
30-
except (ImportError, AttributeError):
29+
original_module = importlib.import_module(handler_module)
30+
except ImportError as e:
3131
raise ImportError(
32-
f"Unable to import module '{handler_module}': No module named '{handler_module}'"
33-
)
32+
f"Runtime.ImportModuleError: Unable to import module '{handler_module}': {str(e)}"
33+
) from None
34+
except SyntaxError as e:
35+
raise SyntaxError(
36+
f"Runtime.UserCodeSyntaxError: Syntax error in module '{handler_module}': {str(e)}"
37+
) from None
38+
try:
39+
original_handler = getattr(original_module, unit_name)
40+
except AttributeError:
41+
raise Exception(
42+
f"Runtime.HandlerNotFound: Handler '{unit_name}' missing on module '{handler_module}'"
43+
) from None
3444
return original_handler(*args, **kwargs)
3545

3646

src/test/unit/test_auto_instrument_handler.py

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,72 @@ def test_hierarchy_happy_flow(monkeypatch):
3232
def test_import_error(monkeypatch):
3333
monkeypatch.setenv(ORIGINAL_HANDLER_KEY, "blabla.not.exists")
3434

35-
with pytest.raises(ImportError):
35+
try:
3636
_handler({}, {})
37+
except ImportError as e:
38+
# Note: We're not using pytest.raises in order to get the exception context
39+
assert "Runtime.ImportModuleError" in str(e)
40+
assert "another exception occurred" not in traceback.format_exc()
41+
else:
42+
assert False
3743

3844

3945
def test_no_env_handler_error(monkeypatch):
40-
if os.environ.get(ORIGINAL_HANDLER_KEY):
41-
monkeypatch.delenv(ORIGINAL_HANDLER_KEY)
46+
monkeypatch.delenv(ORIGINAL_HANDLER_KEY, None)
4247

43-
with pytest.raises(ValueError):
48+
with pytest.raises(Exception) as e:
4449
_handler({}, {})
50+
assert "Could not find the original handler" in str(e.value)
4551

4652

4753
def test_error_in_original_handler_no_extra_exception_log(monkeypatch, context):
48-
monkeypatch.setattr(importlib, "import_module", mock.Mock(side_effect=Exception))
54+
monkeypatch.setattr(importlib, "import_module", mock.Mock(side_effect=ZeroDivisionError))
55+
monkeypatch.setenv(ORIGINAL_HANDLER_KEY, "sys.exit")
56+
57+
try:
58+
_handler({}, context)
59+
except ZeroDivisionError:
60+
# Note: We're not using pytest.raises in order to get the exception context
61+
assert "another exception occurred" not in traceback.format_exc()
62+
else:
63+
assert False
64+
65+
66+
def test_error_in_original_handler_syntax_error(monkeypatch, context):
67+
monkeypatch.setattr(importlib, "import_module", mock.Mock(side_effect=SyntaxError))
4968
monkeypatch.setenv(ORIGINAL_HANDLER_KEY, "sys.exit")
50-
exception_occurred = False
5169

5270
try:
5371
_handler({}, context)
54-
except Exception:
55-
exception_occurred = True
72+
except SyntaxError as e:
73+
# Note: We're not using pytest.raises in order to get the exception context
74+
assert "Runtime.UserCodeSyntaxError" in str(e)
5675
assert "another exception occurred" not in traceback.format_exc()
57-
assert exception_occurred is True
76+
else:
77+
assert False
5878

5979

6080
def test_handler_bad_format(monkeypatch):
6181
monkeypatch.setenv(ORIGINAL_HANDLER_KEY, "no_method")
6282

63-
with pytest.raises(RuntimeError):
83+
try:
84+
_handler({}, {})
85+
except ValueError as e:
86+
# Note: We're not using pytest.raises in order to get the exception context
87+
assert "Runtime.MalformedHandlerName" in str(e)
88+
assert "another exception occurred" not in traceback.format_exc()
89+
else:
90+
assert False
91+
92+
93+
def test_handler_not_found(monkeypatch):
94+
monkeypatch.setenv(ORIGINAL_HANDLER_KEY, "sys.not_found")
95+
96+
try:
6497
_handler({}, {})
98+
except Exception as e:
99+
# Note: We're not using pytest.raises in order to get the exception context
100+
assert "Runtime.HandlerNotFound" in str(e)
101+
assert "another exception occurred" not in traceback.format_exc()
102+
else:
103+
assert False

0 commit comments

Comments
 (0)