Skip to content

Commit d5ca10d

Browse files
authored
fix: modify error message to show which cors key is invalid (#3659)
1 parent edf208e commit d5ca10d

File tree

5 files changed

+45
-7
lines changed

5 files changed

+45
-7
lines changed

samtranslator/model/api/api_generator.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -750,8 +750,9 @@ def _add_cors(self) -> None:
750750
properties = CorsProperties(AllowOrigin=self.cors) # type: ignore[call-arg]
751751
elif isinstance(self.cors, dict):
752752
# Make sure keys in the dict are recognized
753-
if not all(key in CorsProperties._fields for key in self.cors):
754-
raise InvalidResourceException(self.logical_id, INVALID_ERROR)
753+
for key in self.cors:
754+
if key not in CorsProperties._fields:
755+
raise InvalidResourceException(self.logical_id, f"Invalid key '{key}' for 'Cors' property.")
755756

756757
properties = CorsProperties(**self.cors)
757758

samtranslator/model/api/http_api_generator.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,9 @@ def _add_cors(self) -> None:
187187

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

193194
properties = CorsProperties(**self.cors_configuration)
194195

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Resources:
2+
HttpApi:
3+
Type: AWS::Serverless::HttpApi
4+
Properties:
5+
StageName: stagename
6+
DefaultRouteSettings:
7+
ThrottlingBurstLimit: 200
8+
RouteSettings:
9+
GET /path:
10+
ThrottlingBurstLimit: 500 # overridden in HttpApi Event
11+
StageVariables:
12+
StageVar: Value
13+
FailOnWarnings: true
14+
CorsConfiguration:
15+
AllowOrigin:
16+
- https://example.com
17+
AllowHeaders:
18+
- x-apigateway-header
19+
AllowMethods:
20+
- GET
21+
MaxAge: 600
22+
AllowCredentials: true

tests/translator/output/error_invalid_cors_dict.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
"Invalid Serverless Application Specification document. ",
44
"Number of errors found: 1. ",
55
"Resource with id [ServerlessRestApi] is invalid. ",
6-
"Invalid value for 'Cors' property"
6+
"Invalid key 'Foo' for 'Cors' property."
77
],
8-
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [ServerlessRestApi] is invalid. Invalid value for 'Cors' property",
8+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [ServerlessRestApi] is invalid. Invalid key 'Foo' for 'Cors' property.",
99
"errors": [
1010
{
11-
"errorMessage": "Resource with id [ServerlessRestApi] is invalid. Invalid value for 'Cors' property"
11+
"errorMessage": "Resource with id [ServerlessRestApi] is invalid. Invalid key 'Foo' for 'Cors' property."
1212
}
1313
]
1414
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"_autoGeneratedBreakdownErrorMessage": [
3+
"Invalid Serverless Application Specification document. ",
4+
"Number of errors found: 1. ",
5+
"Resource with id [HttpApi] is invalid. ",
6+
"Invalid key 'AllowOrigin' for 'Cors' property."
7+
],
8+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [HttpApi] is invalid. Invalid key 'AllowOrigin' for 'Cors' property.",
9+
"errors": [
10+
{
11+
"errorMessage": "Resource with id [HttpApi] is invalid. Invalid key 'AllowOrigin' for 'Cors' property."
12+
}
13+
]
14+
}

0 commit comments

Comments
 (0)