Skip to content

Commit 056aa36

Browse files
authored
Log warning on unhandled exception. (#120)
* Log warning on unhandled exception.
1 parent b45577e commit 056aa36

File tree

4 files changed

+51
-11
lines changed

4 files changed

+51
-11
lines changed

awslambdaric/bootstrap.py

+5
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ def replace_line_indentation(line, indent_char, new_indent_char):
113113

114114
if _AWS_LAMBDA_LOG_FORMAT == LogFormat.JSON:
115115
_ERROR_FRAME_TYPE = _JSON_FRAME_TYPES[logging.ERROR]
116+
_WARNING_FRAME_TYPE = _JSON_FRAME_TYPES[logging.WARNING]
116117

117118
def log_error(error_result, log_sink):
118119
error_result = {
@@ -128,6 +129,7 @@ def log_error(error_result, log_sink):
128129

129130
else:
130131
_ERROR_FRAME_TYPE = _TEXT_FRAME_TYPES[logging.ERROR]
132+
_WARNING_FRAME_TYPE = _TEXT_FRAME_TYPES[logging.WARNING]
131133

132134
def log_error(error_result, log_sink):
133135
error_description = "[ERROR]"
@@ -210,6 +212,9 @@ def handle_event_request(
210212
)
211213

212214
if error_result is not None:
215+
from .lambda_literals import lambda_unhandled_exception_warning_message
216+
217+
log_sink.log(lambda_unhandled_exception_warning_message, _WARNING_FRAME_TYPE)
213218
log_error(error_result, log_sink)
214219
lambda_runtime_client.post_invocation_error(
215220
invoke_id, to_json(error_result), to_json(xray_fault)

awslambdaric/lambda_literals.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
"""
4+
5+
lambda_warning = "LAMBDA_WARNING"
6+
7+
# Holds warning message that is emitted when an unhandled exception is raised during function invocation.
8+
lambda_unhandled_exception_warning_message = str(
9+
f"{lambda_warning}: "
10+
"Unhandled exception. "
11+
"The most likely cause is an issue in the function code. "
12+
"However, in rare cases, a Lambda runtime update can cause unexpected function behavior. "
13+
"For functions using managed runtimes, runtime updates can be triggered by a function change, or can be applied automatically. "
14+
"To determine if the runtime has been updated, check the runtime version in the INIT_START log entry. "
15+
"If this error correlates with a change in the runtime version, you may be able to mitigate this error by temporarily rolling back to the previous runtime version. "
16+
"For more information, see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-update.html"
17+
)

awslambdaric/lambda_runtime_client.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,11 @@ def post_invocation_result(
108108
):
109109
runtime_client.post_invocation_result(
110110
invoke_id,
111-
result_data
112-
if isinstance(result_data, bytes)
113-
else result_data.encode("utf-8"),
111+
(
112+
result_data
113+
if isinstance(result_data, bytes)
114+
else result_data.encode("utf-8")
115+
),
114116
content_type,
115117
)
116118

tests/test_bootstrap.py

+24-8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
from awslambdaric.lambda_runtime_exception import FaultException
2121
from awslambdaric.lambda_runtime_log_utils import LogFormat, _get_log_level_from_env_var
2222
from awslambdaric.lambda_runtime_marshaller import LambdaMarshaller
23+
from awslambdaric.lambda_literals import (
24+
lambda_unhandled_exception_warning_message,
25+
)
2326

2427

2528
class TestUpdateXrayEnv(unittest.TestCase):
@@ -461,7 +464,10 @@ def raise_exception_handler(json_input, lambda_context):
461464
)
462465

463466
# NOTE: Indentation characters are NO-BREAK SPACE (U+00A0) not SPACE (U+0020)
464-
error_logs = "[ERROR] FaultExceptionType: Fault exception msg\r"
467+
error_logs = (
468+
lambda_unhandled_exception_warning_message
469+
+ "[ERROR] FaultExceptionType: Fault exception msg\r"
470+
)
465471
error_logs += "Traceback (most recent call last):\r"
466472
error_logs += '  File "spam.py", line 3, in <module>\r'
467473
error_logs += "    spam.eggs()\r"
@@ -492,7 +498,10 @@ def raise_exception_handler(json_input, lambda_context):
492498
0,
493499
bootstrap.StandardLogSink(),
494500
)
495-
error_logs = "[ERROR] FaultExceptionType: Fault exception msg\rTraceback (most recent call last):\n"
501+
error_logs = (
502+
lambda_unhandled_exception_warning_message
503+
+ "[ERROR] FaultExceptionType: Fault exception msg\rTraceback (most recent call last):\n"
504+
)
496505

497506
self.assertEqual(mock_stdout.getvalue(), error_logs)
498507

@@ -518,7 +527,10 @@ def raise_exception_handler(json_input, lambda_context):
518527
0,
519528
bootstrap.StandardLogSink(),
520529
)
521-
error_logs = "[ERROR] FaultExceptionType\rTraceback (most recent call last):\n"
530+
error_logs = (
531+
lambda_unhandled_exception_warning_message
532+
+ "[ERROR] FaultExceptionType\rTraceback (most recent call last):\n"
533+
)
522534

523535
self.assertEqual(mock_stdout.getvalue(), error_logs)
524536

@@ -544,7 +556,10 @@ def raise_exception_handler(json_input, lambda_context):
544556
0,
545557
bootstrap.StandardLogSink(),
546558
)
547-
error_logs = "[ERROR] Fault exception msg\rTraceback (most recent call last):\n"
559+
error_logs = (
560+
lambda_unhandled_exception_warning_message
561+
+ "[ERROR] Fault exception msg\rTraceback (most recent call last):\n"
562+
)
548563

549564
self.assertEqual(mock_stdout.getvalue(), error_logs)
550565

@@ -579,8 +594,7 @@ def raise_exception_handler(json_input, lambda_context):
579594
0,
580595
bootstrap.StandardLogSink(),
581596
)
582-
583-
error_logs = "[ERROR]\r"
597+
error_logs = lambda_unhandled_exception_warning_message + "[ERROR]\r"
584598
error_logs += "Traceback (most recent call last):\r"
585599
error_logs += '  File "spam.py", line 3, in <module>\r'
586600
error_logs += "    spam.eggs()\r"
@@ -616,8 +630,10 @@ def test_handle_event_request_fault_exception_logging_syntax_error(
616630
0,
617631
bootstrap.StandardLogSink(),
618632
)
619-
620-
error_logs = f"[ERROR] Runtime.UserCodeSyntaxError: Syntax error in module 'a': {syntax_error}\r"
633+
error_logs = (
634+
lambda_unhandled_exception_warning_message
635+
+ f"[ERROR] Runtime.UserCodeSyntaxError: Syntax error in module 'a': {syntax_error}\r"
636+
)
621637
error_logs += "Traceback (most recent call last):\r"
622638
error_logs += '  File "<string>" Line 1\r'
623639
error_logs += "    -\n"

0 commit comments

Comments
 (0)