Skip to content

Commit 0b2da73

Browse files
authored
fix: InvalidDocumentException accepts array of exceptions not str (#2416)
1 parent 28352b7 commit 0b2da73

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

samtranslator/model/eventsources/push.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@ def _get_permission(self, resources_to_link, stage):
11211121
editor = OpenApiEditor(resources_to_link["explicit_api"].get("DefinitionBody"))
11221122
except InvalidDocumentException as e:
11231123
api_logical_id = self.ApiId.get("Ref") if isinstance(self.ApiId, dict) else self.ApiId
1124-
raise InvalidResourceException(api_logical_id, e)
1124+
raise InvalidResourceException(api_logical_id, " ".join(ex.message for ex in e.causes))
11251125

11261126
# If this is using the new $default path, keep path blank and add a * permission
11271127
if path == OpenApiEditor._DEFAULT_PATH:

samtranslator/open_api/open_api.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ def __init__(self, doc):
4141
"""
4242
if not OpenApiEditor.is_valid(doc):
4343
raise InvalidDocumentException(
44-
"Invalid OpenApi document. "
45-
"Invalid values or missing keys for 'openapi' or 'paths' in 'DefinitionBody'."
44+
[
45+
InvalidTemplateException(
46+
"Invalid OpenApi document. Invalid values or missing keys for 'openapi' or 'paths' in 'DefinitionBody'."
47+
)
48+
]
4649
)
4750

4851
self._doc = copy.deepcopy(doc)

samtranslator/swagger/swagger.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def __init__(self, doc):
4848
"""
4949

5050
if not SwaggerEditor.is_valid(doc):
51-
raise InvalidDocumentException("Invalid Swagger document")
51+
raise InvalidDocumentException([InvalidTemplateException("Invalid Swagger document")])
5252

5353
self._doc = copy.deepcopy(doc)
5454
self.paths = self._doc["paths"]
@@ -187,7 +187,11 @@ def add_lambda_integration(
187187
method = self._normalize_method_name(method)
188188
if self.has_integration(path, method):
189189
raise InvalidDocumentException(
190-
"Lambda integration already exists on Path={}, Method={}".format(path, method)
190+
[
191+
InvalidTemplateException(
192+
"Lambda integration already exists on Path={}, Method={}".format(path, method)
193+
)
194+
]
191195
)
192196

193197
self.add_path(path, method)
@@ -252,7 +256,9 @@ def add_state_machine_integration(
252256

253257
method = self._normalize_method_name(method)
254258
if self.has_integration(path, method):
255-
raise InvalidDocumentException("Integration already exists on Path={}, Method={}".format(path, method))
259+
raise InvalidDocumentException(
260+
[InvalidTemplateException("Integration already exists on Path={}, Method={}".format(path, method))]
261+
)
256262

257263
self.add_path(path, method)
258264

@@ -1029,7 +1035,9 @@ def _add_iam_resource_policy_for_method(self, policy_list, effect, resource_list
10291035
return
10301036

10311037
if effect not in ["Allow", "Deny"]:
1032-
raise InvalidDocumentException("Effect must be one of {}".format(["Allow", "Deny"]))
1038+
raise InvalidDocumentException(
1039+
[InvalidTemplateException("Effect must be one of {}".format(["Allow", "Deny"]))]
1040+
)
10331041

10341042
if not isinstance(policy_list, (dict, list)):
10351043
raise InvalidDocumentException(
@@ -1091,7 +1099,9 @@ def _add_ip_resource_policy_for_method(self, ip_list, conditional, resource_list
10911099
ip_list = [ip_list]
10921100

10931101
if conditional not in ["IpAddress", "NotIpAddress"]:
1094-
raise InvalidDocumentException("Conditional must be one of {}".format(["IpAddress", "NotIpAddress"]))
1102+
raise InvalidDocumentException(
1103+
[InvalidTemplateException("Conditional must be one of {}".format(["IpAddress", "NotIpAddress"]))]
1104+
)
10951105

10961106
self.resource_policy["Version"] = "2012-10-17"
10971107
allow_statement = Py27Dict()
@@ -1127,7 +1137,9 @@ def _add_vpc_resource_policy_for_method(self, endpoint_dict, conditional, resour
11271137
"""
11281138

11291139
if conditional not in ["StringNotEquals", "StringEquals"]:
1130-
raise InvalidDocumentException("Conditional must be one of {}".format(["StringNotEquals", "StringEquals"]))
1140+
raise InvalidDocumentException(
1141+
[InvalidTemplateException("Conditional must be one of {}".format(["StringNotEquals", "StringEquals"]))]
1142+
)
11311143

11321144
condition = Py27Dict()
11331145
string_endpoint_list = endpoint_dict.get("StringEndpointList")

tests/translator/output/error_http_api_invalid_openapi.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
"errorMessage": "Resource with id [Api] is invalid. Invalid OpenApi document. Invalid values or missing keys for 'openapi' or 'paths' in 'DefinitionBody'."
55
}
66
],
7-
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [Api] is invalid. Invalid OpenApi document. Invalid values or missing keys for 'openapi' or 'paths' in 'DefinitionBody'."
7+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [Api] is invalid. Structure of the SAM template is invalid. Invalid OpenApi document. Invalid values or missing keys for 'openapi' or 'paths' in 'DefinitionBody'."
88
}

0 commit comments

Comments
 (0)