From 655f70fbd054c15c982aafd34347400785b225ce Mon Sep 17 00:00:00 2001 From: Pushkar Chawda Date: Fri, 22 Jan 2021 01:28:10 +0000 Subject: [PATCH 1/3] errorMessage missing from exception response, when errorMessage field is empty or None --- awslambdaric/bootstrap.py | 10 +++------- test/test_bootstrap.py | 13 ++++++++----- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/awslambdaric/bootstrap.py b/awslambdaric/bootstrap.py index be42814..ea9c280 100644 --- a/awslambdaric/bootstrap.py +++ b/awslambdaric/bootstrap.py @@ -90,13 +90,9 @@ def result(*args): def make_error(error_message, error_type, stack_trace): - result = {} - if error_message: - result["errorMessage"] = error_message - if error_type: - result["errorType"] = error_type - if stack_trace: - result["stackTrace"] = stack_trace + result = {'errorMessage': error_message if error_message else "", + 'errorType': error_type if error_type else "", + 'stackTrace': stack_trace if stack_trace else []} return result diff --git a/test/test_bootstrap.py b/test/test_bootstrap.py index 3d69038..cb86323 100644 --- a/test/test_bootstrap.py +++ b/test/test_bootstrap.py @@ -435,7 +435,7 @@ def raise_exception_handler(json_input, lambda_context): 0, bootstrap.StandardLogSink(), ) - error_logs = "[ERROR] FaultExceptionType: Fault exception msg\n" + error_logs = "[ERROR] FaultExceptionType: Fault exception msg\rTraceback (most recent call last):\n" self.assertEqual(mock_stdout.getvalue(), error_logs) @@ -461,7 +461,7 @@ def raise_exception_handler(json_input, lambda_context): 0, bootstrap.StandardLogSink(), ) - error_logs = "[ERROR] FaultExceptionType\n" + error_logs = "[ERROR] FaultExceptionType\rTraceback (most recent call last):\n" self.assertEqual(mock_stdout.getvalue(), error_logs) @@ -487,7 +487,7 @@ def raise_exception_handler(json_input, lambda_context): 0, bootstrap.StandardLogSink(), ) - error_logs = "[ERROR] Fault exception msg\n" + error_logs = "[ERROR] Fault exception msg\rTraceback (most recent call last):\n" self.assertEqual(mock_stdout.getvalue(), error_logs) @@ -835,7 +835,7 @@ def test_log_error_standard_log_sink(self, mock_stdout): err_to_log = bootstrap.make_error("Error message", "ErrorType", None) bootstrap.log_error(err_to_log, bootstrap.StandardLogSink()) - expected_logged_error = "[ERROR] ErrorType: Error message\n" + expected_logged_error = "[ERROR] ErrorType: Error message\rTraceback (most recent call last):\n" self.assertEqual(mock_stdout.getvalue(), expected_logged_error) def test_log_error_framed_log_sink(self): @@ -844,7 +844,7 @@ def test_log_error_framed_log_sink(self): err_to_log = bootstrap.make_error("Error message", "ErrorType", None) bootstrap.log_error(err_to_log, log_sink) - expected_logged_error = "[ERROR] ErrorType: Error message" + expected_logged_error = "[ERROR] ErrorType: Error message\nTraceback (most recent call last):" with open(temp_file.name, "rb") as f: content = f.read() @@ -852,6 +852,9 @@ def test_log_error_framed_log_sink(self): frame_type = int.from_bytes(content[:4], "big") self.assertEqual(frame_type, 0xA55A0001) + actual_message = content[8:].decode() + self.assertEqual(actual_message, expected_logged_error) + length = int.from_bytes(content[4:8], "big") self.assertEqual(length, len(expected_logged_error.encode("utf8"))) From fbba42792a7fba1820b12012494135d30b4a74ad Mon Sep 17 00:00:00 2001 From: Pushkar Chawda Date: Fri, 22 Jan 2021 01:47:30 +0000 Subject: [PATCH 2/3] Removed duplicate assert check, added by mistake --- test/test_bootstrap.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/test_bootstrap.py b/test/test_bootstrap.py index cb86323..30cee22 100644 --- a/test/test_bootstrap.py +++ b/test/test_bootstrap.py @@ -852,9 +852,6 @@ def test_log_error_framed_log_sink(self): frame_type = int.from_bytes(content[:4], "big") self.assertEqual(frame_type, 0xA55A0001) - actual_message = content[8:].decode() - self.assertEqual(actual_message, expected_logged_error) - length = int.from_bytes(content[4:8], "big") self.assertEqual(length, len(expected_logged_error.encode("utf8"))) From 8da46608c87163ddf69868433337de7e83079bd3 Mon Sep 17 00:00:00 2001 From: Pushkar Chawda Date: Fri, 22 Jan 2021 14:54:16 +0000 Subject: [PATCH 3/3] Added unit test to test empty errorMessage changes --- awslambdaric/bootstrap.py | 8 ++++-- test/test_bootstrap.py | 60 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/awslambdaric/bootstrap.py b/awslambdaric/bootstrap.py index ea9c280..78e8393 100644 --- a/awslambdaric/bootstrap.py +++ b/awslambdaric/bootstrap.py @@ -90,9 +90,11 @@ def result(*args): def make_error(error_message, error_type, stack_trace): - result = {'errorMessage': error_message if error_message else "", - 'errorType': error_type if error_type else "", - 'stackTrace': stack_trace if stack_trace else []} + result = { + "errorMessage": error_message if error_message else "", + "errorType": error_type if error_type else "", + "stackTrace": stack_trace if stack_trace else [], + } return result diff --git a/test/test_bootstrap.py b/test/test_bootstrap.py index 30cee22..3d4775f 100644 --- a/test/test_bootstrap.py +++ b/test/test_bootstrap.py @@ -296,6 +296,58 @@ def __init__(self, message): self.assertEqual(len(xray_fault["paths"]), 1) self.assertTrue(xray_fault["paths"][0].endswith(os.path.relpath(__file__))) + def test_handle_event_request_custom_empty_error_message_exception(self): + def raise_exception_handler(json_input, lambda_context): + class MyError(Exception): + def __init__(self, message): + self.message = message + + raise MyError("") + + expected_response = {"errorType": "MyError", "errorMessage": ""} + bootstrap.handle_event_request( + self.lambda_runtime, + raise_exception_handler, + "invoke_id", + self.event_body, + "application/json", + {}, + {}, + "invoked_function_arn", + 0, + bootstrap.StandardLogSink(), + ) + args, _ = self.lambda_runtime.post_invocation_error.call_args + error_response = json.loads(args[1]) + self.assertEqual(args[0], "invoke_id") + self.assertTrue( + expected_response.items() <= error_response.items(), + "Expected response is not a subset of the actual response\nExpected: {}\nActual: {}".format( + expected_response, error_response + ), + ) + xray_fault = json.loads(args[2]) + self.assertEqual(xray_fault["working_directory"], self.working_directory) + self.assertEqual(len(xray_fault["exceptions"]), 1) + self.assertEqual( + xray_fault["exceptions"][0]["message"], expected_response["errorMessage"] + ) + self.assertEqual( + xray_fault["exceptions"][0]["type"], expected_response["errorType"] + ) + self.assertEqual(len(xray_fault["exceptions"][0]["stack"]), 1) + self.assertEqual( + xray_fault["exceptions"][0]["stack"][0]["label"], "raise_exception_handler" + ) + self.assertIsInstance(xray_fault["exceptions"][0]["stack"][0]["line"], int) + self.assertTrue( + xray_fault["exceptions"][0]["stack"][0]["path"].endswith( + os.path.relpath(__file__) + ) + ) + self.assertEqual(len(xray_fault["paths"]), 1) + self.assertTrue(xray_fault["paths"][0].endswith(os.path.relpath(__file__))) + def test_handle_event_request_no_module(self): def unable_to_import_module(json_input, lambda_context): import invalid_module @@ -835,7 +887,9 @@ def test_log_error_standard_log_sink(self, mock_stdout): err_to_log = bootstrap.make_error("Error message", "ErrorType", None) bootstrap.log_error(err_to_log, bootstrap.StandardLogSink()) - expected_logged_error = "[ERROR] ErrorType: Error message\rTraceback (most recent call last):\n" + expected_logged_error = ( + "[ERROR] ErrorType: Error message\rTraceback (most recent call last):\n" + ) self.assertEqual(mock_stdout.getvalue(), expected_logged_error) def test_log_error_framed_log_sink(self): @@ -844,7 +898,9 @@ def test_log_error_framed_log_sink(self): err_to_log = bootstrap.make_error("Error message", "ErrorType", None) bootstrap.log_error(err_to_log, log_sink) - expected_logged_error = "[ERROR] ErrorType: Error message\nTraceback (most recent call last):" + expected_logged_error = ( + "[ERROR] ErrorType: Error message\nTraceback (most recent call last):" + ) with open(temp_file.name, "rb") as f: content = f.read()