Skip to content

Commit b782ff2

Browse files
committed
fix: use InvalidDocumentException instead of ValueError
1 parent c1a6690 commit b782ff2

File tree

5 files changed

+27
-26
lines changed

5 files changed

+27
-26
lines changed

samtranslator/model/eventsources/push.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from samtranslator.model.cognito import CognitoUserPool
2020
from samtranslator.translator import logical_id_generator
2121
from samtranslator.translator.arn_generator import ArnGenerator
22-
from samtranslator.model.exceptions import InvalidEventException, InvalidResourceException
22+
from samtranslator.model.exceptions import InvalidEventException, InvalidResourceException, InvalidDocumentException
2323
from samtranslator.swagger.swagger import SwaggerEditor
2424
from samtranslator.open_api.open_api import OpenApiEditor
2525
from samtranslator.utils.py27hash_fix import Py27Dict, Py27UniStr
@@ -1119,7 +1119,7 @@ def _get_permission(self, resources_to_link, stage):
11191119
if resources_to_link["explicit_api"].get("DefinitionBody"):
11201120
try:
11211121
editor = OpenApiEditor(resources_to_link["explicit_api"].get("DefinitionBody"))
1122-
except ValueError as e:
1122+
except InvalidDocumentException as e:
11231123
api_logical_id = self.ApiId.get("Ref") if isinstance(self.ApiId, dict) else self.ApiId
11241124
raise InvalidResourceException(api_logical_id, e)
11251125

samtranslator/open_api/open_api.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ def __init__(self, doc):
3737
modifications on this copy.
3838
3939
:param dict doc: OpenApi document as a dictionary
40-
:raises ValueError: If the input OpenApi document does not meet the basic OpenApi requirements.
40+
:raises InvalidDocumentException: If the input OpenApi document does not meet the basic OpenApi requirements.
4141
"""
4242
if not OpenApiEditor.is_valid(doc):
43-
raise ValueError(
43+
raise InvalidDocumentException(
4444
"Invalid OpenApi document. "
4545
"Invalid values or missing keys for 'openapi' or 'paths' in 'DefinitionBody'."
4646
)
@@ -175,7 +175,7 @@ def add_path(self, path, method=None):
175175
176176
:param string path: Path name
177177
:param string method: HTTP method
178-
:raises ValueError: If the value of `path` in Swagger is not a dictionary
178+
:raises InvalidDocumentException: If the value of `path` in Swagger is not a dictionary
179179
"""
180180
method = self._normalize_method_name(method)
181181

samtranslator/swagger/swagger.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import copy
2-
import json
32
import re
43

54
from samtranslator.model.intrinsics import ref, make_conditional, fnSub, is_intrinsic_no_value
@@ -45,11 +44,11 @@ def __init__(self, doc):
4544
modifications on this copy.
4645
4746
:param dict doc: Swagger document as a dictionary
48-
:raises ValueError: If the input Swagger document does not meet the basic Swagger requirements.
47+
:raises InvalidDocumentException: If the input Swagger document does not meet the basic Swagger requirements.
4948
"""
5049

5150
if not SwaggerEditor.is_valid(doc):
52-
raise ValueError("Invalid Swagger document")
51+
raise InvalidDocumentException("Invalid Swagger document")
5352

5453
self._doc = copy.deepcopy(doc)
5554
self.paths = self._doc["paths"]
@@ -187,7 +186,9 @@ def add_lambda_integration(
187186

188187
method = self._normalize_method_name(method)
189188
if self.has_integration(path, method):
190-
raise ValueError("Lambda integration already exists on Path={}, Method={}".format(path, method))
189+
raise InvalidDocumentException(
190+
"Lambda integration already exists on Path={}, Method={}".format(path, method)
191+
)
191192

192193
self.add_path(path, method)
193194

@@ -251,7 +252,7 @@ def add_state_machine_integration(
251252

252253
method = self._normalize_method_name(method)
253254
if self.has_integration(path, method):
254-
raise ValueError("Integration already exists on Path={}, Method={}".format(path, method))
255+
raise InvalidDocumentException("Integration already exists on Path={}, Method={}".format(path, method))
255256

256257
self.add_path(path, method)
257258

@@ -388,7 +389,7 @@ def add_cors(
388389
:param integer/dict max_age: Maximum duration to cache the CORS Preflight request. Value is set on
389390
Access-Control-Max-Age header. Value can also be an intrinsic function dict.
390391
:param bool/None allow_credentials: Flags whether request is allowed to contain credentials.
391-
:raises ValueError: When values for one of the allowed_* variables is empty
392+
:raises InvalidTemplateException: When values for one of the allowed_* variables is empty
392393
"""
393394

394395
for path_item in self.get_conditional_contents(self.paths.get(path)):
@@ -1022,13 +1023,13 @@ def _add_iam_resource_policy_for_method(self, policy_list, effect, resource_list
10221023
"""
10231024
This method generates a policy statement to grant/deny specific IAM users access to the API method and
10241025
appends it to the swagger under `x-amazon-apigateway-policy`
1025-
:raises ValueError: If the effect passed in does not match the allowed values.
1026+
:raises InvalidDocumentException: If the effect passed in does not match the allowed values.
10261027
"""
10271028
if not policy_list:
10281029
return
10291030

10301031
if effect not in ["Allow", "Deny"]:
1031-
raise ValueError("Effect must be one of {}".format(["Allow", "Deny"]))
1032+
raise InvalidDocumentException("Effect must be one of {}".format(["Allow", "Deny"]))
10321033

10331034
if not isinstance(policy_list, (dict, list)):
10341035
raise InvalidDocumentException(
@@ -1081,7 +1082,7 @@ def _add_ip_resource_policy_for_method(self, ip_list, conditional, resource_list
10811082
"""
10821083
This method generates a policy statement to grant/deny specific IP address ranges access to the API method and
10831084
appends it to the swagger under `x-amazon-apigateway-policy`
1084-
:raises ValueError: If the conditional passed in does not match the allowed values.
1085+
:raises InvalidDocumentException: If the conditional passed in does not match the allowed values.
10851086
"""
10861087
if not ip_list:
10871088
return
@@ -1090,7 +1091,7 @@ def _add_ip_resource_policy_for_method(self, ip_list, conditional, resource_list
10901091
ip_list = [ip_list]
10911092

10921093
if conditional not in ["IpAddress", "NotIpAddress"]:
1093-
raise ValueError("Conditional must be one of {}".format(["IpAddress", "NotIpAddress"]))
1094+
raise InvalidDocumentException("Conditional must be one of {}".format(["IpAddress", "NotIpAddress"]))
10941095

10951096
self.resource_policy["Version"] = "2012-10-17"
10961097
allow_statement = Py27Dict()
@@ -1122,11 +1123,11 @@ def _add_vpc_resource_policy_for_method(self, endpoint_dict, conditional, resour
11221123
"""
11231124
This method generates a policy statement to grant/deny specific VPC/VPCE access to the API method and
11241125
appends it to the swagger under `x-amazon-apigateway-policy`
1125-
:raises ValueError: If the conditional passed in does not match the allowed values.
1126+
:raises InvalidDocumentException: If the conditional passed in does not match the allowed values.
11261127
"""
11271128

11281129
if conditional not in ["StringNotEquals", "StringEquals"]:
1129-
raise ValueError("Conditional must be one of {}".format(["StringNotEquals", "StringEquals"]))
1130+
raise InvalidDocumentException("Conditional must be one of {}".format(["StringNotEquals", "StringEquals"]))
11301131

11311132
condition = Py27Dict()
11321133
string_endpoint_list = endpoint_dict.get("StringEndpointList")

tests/openapi/test_openapi.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ def test_must_raise_on_valid_swagger(self):
2020
"swagger": "2.0", # "openapi": "2.1.0"
2121
"paths": {"/foo": {}, "/bar": {}},
2222
} # missing openapi key word
23-
with self.assertRaises(ValueError):
23+
with self.assertRaises(InvalidDocumentException):
2424
OpenApiEditor(valid_swagger)
2525

2626
def test_must_raise_on_invalid_openapi(self):
2727

2828
invalid_openapi = {"paths": {}} # Missing "openapi" keyword
29-
with self.assertRaises(ValueError):
29+
with self.assertRaises(InvalidDocumentException):
3030
OpenApiEditor(invalid_openapi)
3131

3232
def test_must_succeed_on_valid_openapi(self):
@@ -40,13 +40,13 @@ def test_must_succeed_on_valid_openapi(self):
4040
def test_must_fail_on_invalid_openapi_version(self):
4141
invalid_openapi = {"openapi": "2.3.0", "paths": {"/foo": {}, "/bar": {}}}
4242

43-
with self.assertRaises(ValueError):
43+
with self.assertRaises(InvalidDocumentException):
4444
OpenApiEditor(invalid_openapi)
4545

4646
def test_must_fail_on_invalid_openapi_version_2(self):
4747
invalid_openapi = {"openapi": "3.1.1.1", "paths": {"/foo": {}, "/bar": {}}}
4848

49-
with self.assertRaises(ValueError):
49+
with self.assertRaises(InvalidDocumentException):
5050
OpenApiEditor(invalid_openapi)
5151

5252
def test_must_succeed_on_valid_openapi3(self):

tests/swagger/test_swagger.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class TestSwaggerEditor_init(TestCase):
1818
def test_must_raise_on_invalid_swagger(self):
1919

2020
invalid_swagger = {"paths": {}} # Missing "Swagger" keyword
21-
with self.assertRaises(ValueError):
21+
with self.assertRaises(InvalidDocumentException):
2222
SwaggerEditor(invalid_swagger)
2323

2424
def test_must_succeed_on_valid_swagger(self):
@@ -32,13 +32,13 @@ def test_must_succeed_on_valid_swagger(self):
3232
def test_must_fail_on_invalid_openapi_version(self):
3333
invalid_swagger = {"openapi": "2.3.0", "paths": {"/foo": {}, "/bar": {}}}
3434

35-
with self.assertRaises(ValueError):
35+
with self.assertRaises(InvalidDocumentException):
3636
SwaggerEditor(invalid_swagger)
3737

3838
def test_must_fail_on_invalid_openapi_version_2(self):
3939
invalid_swagger = {"openapi": "3.1.1.1", "paths": {"/foo": {}, "/bar": {}}}
4040

41-
with self.assertRaises(ValueError):
41+
with self.assertRaises(InvalidDocumentException):
4242
SwaggerEditor(invalid_swagger)
4343

4444
def test_must_succeed_on_valid_openapi3(self):
@@ -53,7 +53,7 @@ def test_must_succeed_on_valid_openapi3(self):
5353
def test_must_fail_with_bad_values_for_path(self, invalid_path_item):
5454
invalid_swagger = {"openapi": "3.1.1.1", "paths": {"/foo": {}, "/bad": invalid_path_item}}
5555

56-
with self.assertRaises(ValueError):
56+
with self.assertRaises(InvalidDocumentException):
5757
SwaggerEditor(invalid_swagger)
5858

5959

@@ -261,7 +261,7 @@ def test_must_add_new_integration_to_existing_path(self):
261261

262262
def test_must_raise_on_existing_integration(self):
263263

264-
with self.assertRaises(ValueError):
264+
with self.assertRaises(InvalidDocumentException):
265265
self.editor.add_lambda_integration("/bar", "get", "integrationUri")
266266

267267
def test_must_add_credentials_to_the_integration(self):

0 commit comments

Comments
 (0)