@@ -32,33 +32,72 @@ def test_hierarchy_happy_flow(monkeypatch):
3232def 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
3945def 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
4753def 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
6080def 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