Skip to content

fix: modify error message to show which cors key is invalid #3659

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
Oct 3, 2024
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
5 changes: 3 additions & 2 deletions samtranslator/model/api/api_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,8 +750,9 @@ def _add_cors(self) -> None:
properties = CorsProperties(AllowOrigin=self.cors) # type: ignore[call-arg]
elif isinstance(self.cors, dict):
# Make sure keys in the dict are recognized
if not all(key in CorsProperties._fields for key in self.cors):
raise InvalidResourceException(self.logical_id, INVALID_ERROR)
for key in self.cors:
if key not in CorsProperties._fields:
raise InvalidResourceException(self.logical_id, f"Invalid key '{key}' for 'Cors' property.")

properties = CorsProperties(**self.cors)

Expand Down
5 changes: 3 additions & 2 deletions samtranslator/model/api/http_api_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,9 @@ def _add_cors(self) -> None:

elif isinstance(self.cors_configuration, dict):
# Make sure keys in the dict are recognized
if not all(key in CorsProperties._fields for key in self.cors_configuration):
raise InvalidResourceException(self.logical_id, "Invalid value for 'Cors' property.")
for key in self.cors_configuration:
if key not in CorsProperties._fields:
raise InvalidResourceException(self.logical_id, f"Invalid key '{key}' for 'Cors' property.")

properties = CorsProperties(**self.cors_configuration)

Expand Down
22 changes: 22 additions & 0 deletions tests/translator/input/error_invalid_httpapi_cors_property.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Resources:
HttpApi:
Type: AWS::Serverless::HttpApi
Properties:
StageName: stagename
DefaultRouteSettings:
ThrottlingBurstLimit: 200
RouteSettings:
GET /path:
ThrottlingBurstLimit: 500 # overridden in HttpApi Event
StageVariables:
StageVar: Value
FailOnWarnings: true
CorsConfiguration:
AllowOrigin:
- https://example.com
AllowHeaders:
- x-apigateway-header
AllowMethods:
- GET
MaxAge: 600
AllowCredentials: true
6 changes: 3 additions & 3 deletions tests/translator/output/error_invalid_cors_dict.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
"Invalid Serverless Application Specification document. ",
"Number of errors found: 1. ",
"Resource with id [ServerlessRestApi] is invalid. ",
"Invalid value for 'Cors' property"
"Invalid key 'Foo' for 'Cors' property."
],
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [ServerlessRestApi] is invalid. Invalid value for 'Cors' property",
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [ServerlessRestApi] is invalid. Invalid key 'Foo' for 'Cors' property.",
"errors": [
{
"errorMessage": "Resource with id [ServerlessRestApi] is invalid. Invalid value for 'Cors' property"
"errorMessage": "Resource with id [ServerlessRestApi] is invalid. Invalid key 'Foo' for 'Cors' property."
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"_autoGeneratedBreakdownErrorMessage": [
"Invalid Serverless Application Specification document. ",
"Number of errors found: 1. ",
"Resource with id [HttpApi] is invalid. ",
"Invalid key 'AllowOrigin' for 'Cors' property."
],
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [HttpApi] is invalid. Invalid key 'AllowOrigin' for 'Cors' property.",
"errors": [
{
"errorMessage": "Resource with id [HttpApi] is invalid. Invalid key 'AllowOrigin' for 'Cors' property."
}
]
}
Loading