Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: catch improper event type errors #846

Merged
merged 3 commits into from
Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions samtranslator/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,8 @@ def resolve_resource_type(self, resource_dict):
:rtype: class
"""
if not self.can_resolve(resource_dict):
raise TypeError("Resource dict has missing or invalid value for key Type. Resource Dict is: " +
str(resource_dict))
raise TypeError("Resource dict has missing or invalid value for key Type. Event Type is: {}.".format(
resource_dict.get('Type')))
if resource_dict['Type'] not in self.resource_types:
raise TypeError("Invalid resource type {resource_type}".format(resource_type=resource_dict['Type']))
return self.resource_types[resource_dict['Type']]
14 changes: 10 additions & 4 deletions samtranslator/model/sam_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,11 @@ def _event_resources_to_link(self, resources):
event_resources = {}
if self.Events:
for logical_id, event_dict in self.Events.items():
event_source = self.event_resolver.resolve_resource_type(event_dict).from_dict(
self.logical_id + logical_id, event_dict, logical_id)
try:
event_source = self.event_resolver.resolve_resource_type(event_dict).from_dict(
self.logical_id + logical_id, event_dict, logical_id)
except TypeError as e:
raise InvalidEventException(logical_id, "{}".format(e))
event_resources[logical_id] = event_source.resources_to_link(resources)
return event_resources

Expand All @@ -286,8 +289,11 @@ def _generate_event_resources(self, lambda_function, execution_role, event_resou
resources = []
if self.Events:
for logical_id, event_dict in self.Events.items():
eventsource = self.event_resolver.resolve_resource_type(event_dict).from_dict(
lambda_function.logical_id + logical_id, event_dict, logical_id)
try:
eventsource = self.event_resolver.resolve_resource_type(event_dict).from_dict(
lambda_function.logical_id + logical_id, event_dict, logical_id)
except TypeError as e:
raise InvalidEventException(logical_id, "{}".format(e))

kwargs = {
# When Alias is provided, connect all event sources to the alias and *not* the function
Expand Down
26 changes: 26 additions & 0 deletions tests/translator/input/error_function_invalid_event_type.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Resources:
FunctionApiTypeError:
Type: 'AWS::Serverless::Function'
Properties:
CodeUri: s3://sam-demo-bucket/hello.zip
Handler: hello.handler
Runtime: python2.7
Events:
ApiEvent:
Type: API
Properties:
Method: get
Path: /

FunctionNoEventType:
Type: 'AWS::Serverless::Function'
Properties:
CodeUri: s3://sam-demo-bucket/hello.zip
Handler: hello.handler
Runtime: python2.7
Events:
MissingType:
Properties:
Method: get
Path: /

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"errors": [
{
"errorMessage": "Resource with id [FunctionApiTypeError] is invalid. Event with id [ApiEvent] is invalid. Resource dict has missing or invalid value for key Type. Event Type is: API. Resource with id [FunctionNoEventType] is invalid. Event with id [MissingType] is invalid. Resource dict has missing or invalid value for key Type. Event Type is: None."
}
],
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 2. Resource with id [FunctionApiTypeError] is invalid. Event with id [ApiEvent] is invalid. Resource dict has missing or invalid value for key Type. Event Type is: API. Resource with id [FunctionNoEventType] is invalid. Event with id [MissingType] is invalid. Resource dict has missing or invalid value for key Type. Event Type is: None."
}
1 change: 1 addition & 0 deletions tests/translator/test_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ def _generate_new_deployment_hash(self, logical_id, dict_to_hash, rest_api_to_sw
'error_cors_credentials_true_with_wildcard_origin',
'error_cors_credentials_true_without_explicit_origin',
'error_function_invalid_codeuri',
'error_function_invalid_event_type',
'error_function_invalid_layer',
'error_function_no_codeuri',
'error_function_no_handler',
Expand Down