Skip to content

errorMessage field is missing from exception response #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions awslambdaric/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,11 @@ 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


Expand Down
66 changes: 61 additions & 5 deletions test/test_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -435,7 +487,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)

Expand All @@ -461,7 +513,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)

Expand All @@ -487,7 +539,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)

Expand Down Expand Up @@ -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\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):
Expand All @@ -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"
expected_logged_error = (
"[ERROR] ErrorType: Error message\nTraceback (most recent call last):"
)

with open(temp_file.name, "rb") as f:
content = f.read()
Expand Down