Skip to content

Commit 86522b2

Browse files
authored
fix: use formatting instead of concatenating when preparing the exception messages (#2369)
* fix: use formatting instead of concatenating when preparing the exception messages * fix: use formatting instead of concatenating when preparing the exception messages
1 parent a3c40bb commit 86522b2

File tree

5 files changed

+20
-13
lines changed

5 files changed

+20
-13
lines changed

samtranslator/model/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ def get_runtime_attr(self, attr_name):
327327
if attr_name in self.runtime_attrs:
328328
return self.runtime_attrs[attr_name](self)
329329
else:
330-
raise NotImplementedError(attr_name + " attribute is not implemented for resource " + self.resource_type)
330+
raise NotImplementedError(f"{attr_name} attribute is not implemented for resource {self.resource_type}")
331331

332332
def get_passthrough_resource_attributes(self):
333333
"""
@@ -447,7 +447,7 @@ def _check_tag(self, reserved_tag_name, tags):
447447
if reserved_tag_name in tags:
448448
raise InvalidResourceException(
449449
self.logical_id,
450-
reserved_tag_name + " is a reserved Tag key name and "
450+
f"{reserved_tag_name} is a reserved Tag key name and "
451451
"cannot be set on your resource. "
452452
"Please change the tag key in the "
453453
"input.",

samtranslator/model/apigateway.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,13 @@ def __init__(
247247
if function_payload_type not in ApiGatewayAuthorizer._VALID_FUNCTION_PAYLOAD_TYPES:
248248
raise InvalidResourceException(
249249
api_logical_id,
250-
name + " Authorizer has invalid " "'FunctionPayloadType': " + function_payload_type + ".",
250+
f"{name} Authorizer has invalid 'FunctionPayloadType': {function_payload_type}.",
251251
)
252252

253253
if function_payload_type == "REQUEST" and self._is_missing_identity_source(identity):
254254
raise InvalidResourceException(
255255
api_logical_id,
256-
name + " Authorizer must specify Identity with at least one "
256+
f"{name} Authorizer must specify Identity with at least one "
257257
"of Headers, QueryStrings, StageVariables, or Context.",
258258
)
259259

samtranslator/model/apigatewayv2.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,26 +151,26 @@ def _validate_input_parameters(self):
151151
def _validate_jwt_authorizer(self):
152152
if not self.jwt_configuration:
153153
raise InvalidResourceException(
154-
self.api_logical_id, self.name + " OAuth2 Authorizer must define 'JwtConfiguration'."
154+
self.api_logical_id, f"{self.name} OAuth2 Authorizer must define 'JwtConfiguration'."
155155
)
156156
if not self.id_source:
157157
raise InvalidResourceException(
158-
self.api_logical_id, self.name + " OAuth2 Authorizer must define 'IdentitySource'."
158+
self.api_logical_id, f"{self.name} OAuth2 Authorizer must define 'IdentitySource'."
159159
)
160160

161161
def _validate_lambda_authorizer(self):
162162
if not self.function_arn:
163163
raise InvalidResourceException(
164-
self.api_logical_id, self.name + " Lambda Authorizer must define 'FunctionArn'."
164+
self.api_logical_id, f"{self.name} Lambda Authorizer must define 'FunctionArn'."
165165
)
166166
if not self.authorizer_payload_format_version:
167167
raise InvalidResourceException(
168-
self.api_logical_id, self.name + " Lambda Authorizer must define 'AuthorizerPayloadFormatVersion'."
168+
self.api_logical_id, f"{self.name} Lambda Authorizer must define 'AuthorizerPayloadFormatVersion'."
169169
)
170170

171171
if self.identity and not isinstance(self.identity, dict):
172172
raise InvalidResourceException(
173-
self.api_logical_id, self.name + " Lambda Authorizer property 'identity' is of invalid type."
173+
self.api_logical_id, f"{self.name} Lambda Authorizer property 'identity' is of invalid type."
174174
)
175175

176176
def generate_openapi(self):

samtranslator/plugins/api/implicit_api_plugin.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,8 @@ def _add_api_to_swagger(self, event_id, event_properties, template):
189189
if isinstance(api_id, dict) or is_referencing_http_from_api_event:
190190
raise InvalidEventException(
191191
event_id,
192-
self.api_id_property
193-
+ " must be a valid reference to an '"
194-
+ self._get_api_resource_type_name()
195-
+ "' resource in same template.",
192+
f"{self.api_id_property} must be a valid reference to an '{self._get_api_resource_type_name()}'"
193+
" resource in same template.",
196194
)
197195

198196
# Make sure Swagger is valid

tests/model/test_api.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from samtranslator.model import InvalidResourceException
55
from samtranslator.model.apigateway import ApiGatewayAuthorizer
6+
from samtranslator.utils.py27hash_fix import Py27Dict
67

78

89
class TestApiGatewayAuthorizer(TestCase):
@@ -25,6 +26,14 @@ def test_create_authorizer_fails_with_missing_identity_values_and_not_cached(sel
2526
function_payload_type="REQUEST",
2627
)
2728

29+
def test_create_authorizer_fails_with_invalid_function_payload_type(self):
30+
with self.assertRaises(InvalidResourceException):
31+
ApiGatewayAuthorizer(
32+
api_logical_id="logicalId",
33+
name="authName",
34+
function_payload_type=Py27Dict({"key": "value"}),
35+
)
36+
2837
def test_create_authorizer_fails_with_empty_identity(self):
2938
with pytest.raises(InvalidResourceException):
3039
ApiGatewayAuthorizer(

0 commit comments

Comments
 (0)