Skip to content

Commit bc8170a

Browse files
authoredJun 16, 2021
Merge pull request #40 from preearor/main
Add requestId in error response
2 parents 66f1e9d + cab1826 commit bc8170a

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed
 

‎awslambdaric/bootstrap.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,11 @@ def result(*args):
7373
return result
7474

7575

76-
def make_error(error_message, error_type, stack_trace):
76+
def make_error(error_message, error_type, stack_trace, invoke_id=None):
7777
result = {
7878
"errorMessage": error_message if error_message else "",
7979
"errorType": error_type if error_type else "",
80+
"requestId": invoke_id if invoke_id is not None else "",
8081
"stackTrace": stack_trace if stack_trace else [],
8182
}
8283
return result
@@ -151,7 +152,7 @@ def handle_event_request(
151152
)
152153
except FaultException as e:
153154
xray_fault = make_xray_fault("LambdaValidationError", e.msg, os.getcwd(), [])
154-
error_result = make_error(e.msg, e.exception_type, e.trace)
155+
error_result = make_error(e.msg, e.exception_type, e.trace, invoke_id)
155156

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

164165
xray_fault = make_xray_fault(etype.__name__, str(value), os.getcwd(), tb_tuples)
165166
error_result = make_error(
166-
str(value), etype.__name__, traceback.format_list(tb_tuples)
167+
str(value), etype.__name__, traceback.format_list(tb_tuples), invoke_id
167168
)
168169

169170
if error_result is not None:

‎tests/test_bootstrap.py

+30
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ def raise_exception_handler(json_input, lambda_context):
392392
expected_response = {
393393
"errorType": "FaultExceptionType",
394394
"errorMessage": "Fault exception msg",
395+
"requestId": "invoke_id",
395396
"stackTrace": ["trace_line1\ntrace_line2", "trace_line3\ntrace_line4"],
396397
}
397398
bootstrap.handle_event_request(
@@ -988,6 +989,35 @@ def test_log_error_empty_stacktrace_line_framed_log_sink(self):
988989
actual_message = content[8:].decode()
989990
self.assertEqual(actual_message, expected_logged_error)
990991

992+
# Just to ensure we are not logging the requestId from error response, just sending in the response
993+
def test_log_error_invokeId_line_framed_log_sink(self):
994+
with NamedTemporaryFile() as temp_file:
995+
with bootstrap.FramedTelemetryLogSink(temp_file.name) as log_sink:
996+
err_to_log = bootstrap.make_error(
997+
"Error message",
998+
"ErrorType",
999+
["line1", "", "line2"],
1000+
"testrequestId",
1001+
)
1002+
bootstrap.log_error(err_to_log, log_sink)
1003+
1004+
expected_logged_error = (
1005+
"[ERROR] ErrorType: Error message\nTraceback "
1006+
"(most recent call last):\nline1\n\nline2"
1007+
)
1008+
1009+
with open(temp_file.name, "rb") as f:
1010+
content = f.read()
1011+
1012+
frame_type = int.from_bytes(content[:4], "big")
1013+
self.assertEqual(frame_type, 0xA55A0001)
1014+
1015+
length = int.from_bytes(content[4:8], "big")
1016+
self.assertEqual(length, len(expected_logged_error))
1017+
1018+
actual_message = content[8:].decode()
1019+
self.assertEqual(actual_message, expected_logged_error)
1020+
9911021

9921022
class TestUnbuffered(unittest.TestCase):
9931023
def test_write(self):

0 commit comments

Comments
 (0)
Please sign in to comment.