Skip to content

Commit 5c6d766

Browse files
committed
fix: throw ValidationException instead of InvalidRequestContent when function name is invalid
1 parent 987223c commit 5c6d766

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

samcli/local/lambda_service/lambda_error_responses.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ class LambdaErrorResponses:
1919
# The request body could not be parsed as JSON.
2020
InvalidRequestContentException = ("InvalidRequestContent", 400)
2121

22+
# One or more parameters values were invalid.
23+
ValidationException = ("ValidationException", 400)
24+
2225
NotImplementedException = ("NotImplemented", 501)
2326

2427
ContainerCreationFailed = ("ContainerCreationFailed", 501)
@@ -85,6 +88,29 @@ def invalid_request_content(message):
8588
exception_tuple[1],
8689
)
8790

91+
@staticmethod
92+
def validation_exception(message):
93+
"""
94+
Creates a Lambda Service ValidationException Response
95+
96+
Parameters
97+
----------
98+
message str
99+
Message to be added to the body of the response
100+
101+
Returns
102+
-------
103+
Flask.Response
104+
A response object representing the ValidationException Error
105+
"""
106+
exception_tuple = LambdaErrorResponses.ValidationException
107+
108+
return BaseLocalService.service_response(
109+
LambdaErrorResponses._construct_error_response_body(LambdaErrorResponses.USER_ERROR, message),
110+
LambdaErrorResponses._construct_headers(exception_tuple[0]),
111+
exception_tuple[1],
112+
)
113+
88114
@staticmethod
89115
def unsupported_media_type(content_type):
90116
"""

samcli/local/lambda_service/local_lambda_invoke_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def _invoke_request_handler(self, function_name):
172172
normalized_function_name = normalize_lambda_function_name(function_name)
173173
except InvalidFunctionNameException as e:
174174
LOG.error("Invalid function name: %s", str(e))
175-
return LambdaErrorResponses.invalid_request_content(str(e))
175+
return LambdaErrorResponses.validation_exception(str(e))
176176

177177
stdout_stream_string = io.StringIO()
178178
stdout_stream_bytes = io.BytesIO()

tests/unit/local/lambda_service/test_lambda_error_responses.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,18 @@ def test_invalid_request_content(self, service_response_mock):
2626
response = LambdaErrorResponses.invalid_request_content("InvalidRequestContent")
2727

2828
self.assertEqual(response, "InvalidRequestContent")
29+
30+
@patch("samcli.local.services.base_local_service.BaseLocalService.service_response")
31+
def test_validation_exception(self, service_response_mock):
32+
service_response_mock.return_value = "ValidationException"
33+
34+
response = LambdaErrorResponses.validation_exception("ValidationException")
35+
36+
self.assertEqual(response, "ValidationException")
37+
2938
service_response_mock.assert_called_once_with(
30-
'{"Type": "User", "Message": "InvalidRequestContent"}',
31-
{"x-amzn-errortype": "InvalidRequestContent", "Content-Type": "application/json"},
39+
'{"Type": "User", "Message": "ValidationException"}',
40+
{"x-amzn-errortype": "ValidationException", "Content-Type": "application/json"},
3241
400,
3342
)
3443

tests/unit/local/lambda_service/test_local_lambda_invoke_service.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,25 @@ def test_normalize_function_name_with_non_lambda_arn(self):
379379
with pytest.raises(InvalidFunctionNameException):
380380
normalize_lambda_function_name(s3_arn)
381381

382+
@patch("samcli.local.lambda_service.local_lambda_invoke_service.normalize_lambda_function_name")
383+
@patch("samcli.local.lambda_service.local_lambda_invoke_service.LambdaErrorResponses")
384+
def test_invoke_request_handler_with_invalid_function_name(self, lambda_error_responses_mock, normalize_mock):
385+
"""Test that InvalidFunctionNameException returns ValidationException"""
386+
normalize_mock.side_effect = InvalidFunctionNameException("Invalid function name")
387+
lambda_error_responses_mock.validation_exception.return_value = "ValidationException"
388+
389+
request_mock = Mock()
390+
request_mock.get_data.return_value = b'{"key": "value"}'
391+
392+
lambda_runner_mock = Mock()
393+
service = LocalLambdaInvokeService(lambda_runner=lambda_runner_mock, port=3001, host="127.0.0.1")
394+
395+
with patch("samcli.local.lambda_service.local_lambda_invoke_service.request", request_mock):
396+
response = service._invoke_request_handler("invalid:function:name")
397+
398+
self.assertEqual(response, "ValidationException")
399+
lambda_error_responses_mock.validation_exception.assert_called_once_with("Invalid function name")
400+
382401
@patch("samcli.local.lambda_service.local_lambda_invoke_service.LocalLambdaInvokeService.service_response")
383402
@patch("samcli.local.lambda_service.local_lambda_invoke_service.LambdaOutputParser")
384403
def test_invoke_request_handler_with_arn(self, lambda_output_parser_mock, service_response_mock):

0 commit comments

Comments
 (0)