Skip to content

Add requestId in error response #40

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 1 commit into from
Jun 16, 2021
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
7 changes: 4 additions & 3 deletions awslambdaric/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ def result(*args):
return result


def make_error(error_message, error_type, stack_trace):
def make_error(error_message, error_type, stack_trace, invoke_id=None):
result = {
"errorMessage": error_message if error_message else "",
"errorType": error_type if error_type else "",
"requestId": invoke_id if invoke_id is not None else "",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this can be simplified:

 "requestId": invoke_id if invoke_id else "",

"stackTrace": stack_trace if stack_trace else [],
}
return result
Expand Down Expand Up @@ -151,7 +152,7 @@ def handle_event_request(
)
except FaultException as e:
xray_fault = make_xray_fault("LambdaValidationError", e.msg, os.getcwd(), [])
error_result = make_error(e.msg, e.exception_type, e.trace)
error_result = make_error(e.msg, e.exception_type, e.trace, invoke_id)

except Exception:
etype, value, tb = sys.exc_info()
Expand All @@ -163,7 +164,7 @@ def handle_event_request(

xray_fault = make_xray_fault(etype.__name__, str(value), os.getcwd(), tb_tuples)
error_result = make_error(
str(value), etype.__name__, traceback.format_list(tb_tuples)
str(value), etype.__name__, traceback.format_list(tb_tuples), invoke_id
)

if error_result is not None:
Expand Down
30 changes: 30 additions & 0 deletions tests/test_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ def raise_exception_handler(json_input, lambda_context):
expected_response = {
"errorType": "FaultExceptionType",
"errorMessage": "Fault exception msg",
"requestId": "invoke_id",
"stackTrace": ["trace_line1\ntrace_line2", "trace_line3\ntrace_line4"],
}
bootstrap.handle_event_request(
Expand Down Expand Up @@ -988,6 +989,35 @@ def test_log_error_empty_stacktrace_line_framed_log_sink(self):
actual_message = content[8:].decode()
self.assertEqual(actual_message, expected_logged_error)

# Just to ensure we are not logging the requestId from error response, just sending in the response
def test_log_error_invokeId_line_framed_log_sink(self):
with NamedTemporaryFile() as temp_file:
with bootstrap.FramedTelemetryLogSink(temp_file.name) as log_sink:
err_to_log = bootstrap.make_error(
"Error message",
"ErrorType",
["line1", "", "line2"],
"testrequestId",
)
bootstrap.log_error(err_to_log, log_sink)

expected_logged_error = (
"[ERROR] ErrorType: Error message\nTraceback "
"(most recent call last):\nline1\n\nline2"
)

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

frame_type = int.from_bytes(content[:4], "big")
self.assertEqual(frame_type, 0xA55A0001)

length = int.from_bytes(content[4:8], "big")
self.assertEqual(length, len(expected_logged_error))
Comment on lines +1012 to +1016
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: there are separate tests to ensure the framing is correct, these assertions are redundant here


actual_message = content[8:].decode()
self.assertEqual(actual_message, expected_logged_error)


class TestUnbuffered(unittest.TestCase):
def test_write(self):
Expand Down