Skip to content

Commit

Permalink
fix: modify error message to show which cors key is invalid (#3659)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaythapa authored Oct 3, 2024
1 parent edf208e commit d5ca10d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
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."
}
]
}
14 changes: 14 additions & 0 deletions tests/translator/output/error_invalid_httpapi_cors_property.json
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."
}
]
}

0 comments on commit d5ca10d

Please sign in to comment.