Skip to content

Commit a37a43a

Browse files
authored
Propogate error type in header when reporting init error to RAPID (#166)
1 parent f9d370f commit a37a43a

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

awslambdaric/bootstrap.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ def run(app_root, handler, lambda_runtime_api_addr):
480480

481481
if error_result is not None:
482482
log_error(error_result, log_sink)
483-
lambda_runtime_client.post_init_error(to_json(error_result))
483+
lambda_runtime_client.post_init_error(error_result)
484484

485485
sys.exit(1)
486486

awslambdaric/lambda_runtime_client.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import sys
66
from awslambdaric import __version__
77
from .lambda_runtime_exception import FaultException
8+
from .lambda_runtime_marshaller import to_json
9+
10+
ERROR_TYPE_HEADER = "Lambda-Runtime-Function-Error-Type"
811

912

1013
def _user_agent():
@@ -68,7 +71,10 @@ def post_init_error(self, error_response_data):
6871
runtime_connection = http.client.HTTPConnection(self.lambda_runtime_address)
6972
runtime_connection.connect()
7073
endpoint = "/2018-06-01/runtime/init/error"
71-
runtime_connection.request("POST", endpoint, error_response_data)
74+
headers = {ERROR_TYPE_HEADER: error_response_data["errorType"]}
75+
runtime_connection.request(
76+
"POST", endpoint, to_json(error_response_data), headers=headers
77+
)
7278
response = runtime_connection.getresponse()
7379
response_body = response.read()
7480

tests/test_lambda_runtime_client.py

+18-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
LambdaRuntimeClientError,
1515
_user_agent,
1616
)
17+
from awslambdaric.lambda_runtime_marshaller import to_json
1718

1819

1920
class TestInvocationRequest(unittest.TestCase):
@@ -99,6 +100,15 @@ def test_wait_next_invocation(self, mock_runtime_client):
99100
self.assertEqual(event_request.content_type, "application/json")
100101
self.assertEqual(event_request.event_body, response_body)
101102

103+
error_result = {
104+
"errorMessage": "Dummy message",
105+
"errorType": "Runtime.DummyError",
106+
"requestId": "",
107+
"stackTrace": [],
108+
}
109+
110+
headers = {"Lambda-Runtime-Function-Error-Type": error_result["errorType"]}
111+
102112
@patch("http.client.HTTPConnection", autospec=http.client.HTTPConnection)
103113
def test_post_init_error(self, MockHTTPConnection):
104114
mock_conn = MockHTTPConnection.return_value
@@ -108,11 +118,14 @@ def test_post_init_error(self, MockHTTPConnection):
108118
mock_response.code = http.HTTPStatus.ACCEPTED
109119

110120
runtime_client = LambdaRuntimeClient("localhost:1234")
111-
runtime_client.post_init_error("error_data")
121+
runtime_client.post_init_error(self.error_result)
112122

113123
MockHTTPConnection.assert_called_with("localhost:1234")
114124
mock_conn.request.assert_called_once_with(
115-
"POST", "/2018-06-01/runtime/init/error", "error_data"
125+
"POST",
126+
"/2018-06-01/runtime/init/error",
127+
to_json(self.error_result),
128+
headers=self.headers,
116129
)
117130
mock_response.read.assert_called_once()
118131

@@ -127,7 +140,7 @@ def test_post_init_error_non_accepted_status_code(self, MockHTTPConnection):
127140
runtime_client = LambdaRuntimeClient("localhost:1234")
128141

129142
with self.assertRaises(LambdaRuntimeClientError) as cm:
130-
runtime_client.post_init_error("error_data")
143+
runtime_client.post_init_error(self.error_result)
131144
returned_exception = cm.exception
132145

133146
self.assertEqual(returned_exception.endpoint, "/2018-06-01/runtime/init/error")
@@ -215,12 +228,12 @@ def test_post_invocation_error_with_too_large_xray_cause(self, mock_runtime_clie
215228
def test_connection_refused(self):
216229
with self.assertRaises(ConnectionRefusedError):
217230
runtime_client = LambdaRuntimeClient("127.0.0.1:1")
218-
runtime_client.post_init_error("error")
231+
runtime_client.post_init_error(self.error_result)
219232

220233
def test_invalid_addr(self):
221234
with self.assertRaises(OSError):
222235
runtime_client = LambdaRuntimeClient("::::")
223-
runtime_client.post_init_error("error")
236+
runtime_client.post_init_error(self.error_result)
224237

225238
def test_lambdaric_version(self):
226239
self.assertTrue(_user_agent().endswith(__version__))

0 commit comments

Comments
 (0)