-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
feat(LocalLambdaService): Error handling for local lambda #532
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
503119d
feat(LocalLambdaService): Error handling for local lambda
jfuss 72232aa
Handled some PR feedback and finished functional tests for LocalLambd…
jfuss 6f01f5b
Move CaseInsensiveDict test to test_base_local_service and add tests …
jfuss f8524b7
Updated and add tests for LocalLambdaInvokeService for error handling…
jfuss 7d4dc19
Added unit tests for LambdaErrorResponses class
jfuss 0755bc0
Fix linting
jfuss 47cbb47
Update tests to support Py2 and Py3 differences
jfuss 8760cde
Added docs to BaseLocalService.is_lambda_user_error_response
jfuss ee9f3d5
Merge branch 'develop' into lls-error-handling
sanathkr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,242 @@ | ||
"""Common Lambda Error Responses""" | ||
|
||
import json | ||
from collections import OrderedDict | ||
|
||
from samcli.local.services.base_local_service import BaseLocalService | ||
|
||
|
||
class LambdaErrorResponses(object): | ||
|
||
# The content type of the Invoke request body is not JSON. | ||
UnsupportedMediaTypeException = ('UnsupportedMediaType', 415) | ||
|
||
# The AWS Lambda service encountered an internal error. | ||
ServiceException = ('Service', 500) | ||
|
||
# The resource (for example, a Lambda function or access policy statement) specified in the request does not exist. | ||
ResourceNotFoundException = ('ResourceNotFound', 404) | ||
|
||
# The request body could not be parsed as JSON. | ||
InvalidRequestContentException = ('InvalidRequestContent', 400) | ||
|
||
NotImplementedException = ('NotImplemented', 501) | ||
|
||
PathNotFoundException = ('PathNotFoundLocally', 404) | ||
|
||
MethodNotAllowedException = ('MethodNotAllowedLocally', 405) | ||
|
||
# Error Types | ||
USER_ERROR = "User" | ||
SERVICE_ERROR = "Service" | ||
LOCAL_SERVICE_ERROR = "LocalService" | ||
|
||
# Header Information | ||
CONTENT_TYPE = 'application/json' | ||
CONTENT_TYPE_HEADER_KEY = 'Content-Type' | ||
|
||
@staticmethod | ||
def resource_not_found(function_name): | ||
""" | ||
Creates a Lambda Service ResourceNotFound Response | ||
|
||
Parameters | ||
---------- | ||
function_name str | ||
Name of the function that was requested to invoke | ||
|
||
Returns | ||
------- | ||
Flask.Response | ||
A response object representing the ResourceNotFound Error | ||
""" | ||
exception_tuple = LambdaErrorResponses.ResourceNotFoundException | ||
|
||
return BaseLocalService.service_response( | ||
LambdaErrorResponses._construct_error_response_body( | ||
LambdaErrorResponses.USER_ERROR, | ||
"Function not found: arn:aws:lambda:us-west-2:012345678901:function:{}".format(function_name) | ||
), | ||
LambdaErrorResponses._construct_headers(exception_tuple[0]), | ||
exception_tuple[1] | ||
) | ||
|
||
@staticmethod | ||
def invalid_request_content(message): | ||
""" | ||
Creates a Lambda Service InvalidRequestContent Response | ||
|
||
Parameters | ||
---------- | ||
message str | ||
Message to be added to the body of the response | ||
|
||
Returns | ||
------- | ||
Flask.Response | ||
A response object representing the InvalidRequestContent Error | ||
""" | ||
exception_tuple = LambdaErrorResponses.InvalidRequestContentException | ||
|
||
return BaseLocalService.service_response( | ||
LambdaErrorResponses._construct_error_response_body(LambdaErrorResponses.USER_ERROR, message), | ||
LambdaErrorResponses._construct_headers(exception_tuple[0]), | ||
exception_tuple[1] | ||
) | ||
|
||
@staticmethod | ||
def unsupported_media_type(content_type): | ||
""" | ||
Creates a Lambda Service UnsupportedMediaType Response | ||
|
||
Parameters | ||
---------- | ||
content_type str | ||
Content Type of the request that was made | ||
|
||
Returns | ||
------- | ||
Flask.Response | ||
A response object representing the UnsupportedMediaType Error | ||
""" | ||
exception_tuple = LambdaErrorResponses.UnsupportedMediaTypeException | ||
|
||
return BaseLocalService.service_response( | ||
LambdaErrorResponses._construct_error_response_body(LambdaErrorResponses.USER_ERROR, | ||
"Unsupported content type: {}".format(content_type)), | ||
LambdaErrorResponses._construct_headers(exception_tuple[0]), | ||
exception_tuple[1] | ||
) | ||
|
||
@staticmethod | ||
def generic_service_exception(*args): | ||
""" | ||
Creates a Lambda Service Generic ServiceException Response | ||
|
||
Parameters | ||
---------- | ||
args list | ||
List of arguments Flask passes to the method | ||
|
||
Returns | ||
------- | ||
Flask.Response | ||
A response object representing the GenericServiceException Error | ||
""" | ||
exception_tuple = LambdaErrorResponses.ServiceException | ||
|
||
return BaseLocalService.service_response( | ||
LambdaErrorResponses._construct_error_response_body(LambdaErrorResponses.SERVICE_ERROR, "ServiceException"), | ||
LambdaErrorResponses._construct_headers(exception_tuple[0]), | ||
exception_tuple[1] | ||
) | ||
|
||
@staticmethod | ||
def not_implemented_locally(message): | ||
""" | ||
Creates a Lambda Service NotImplementedLocally Response | ||
|
||
Parameters | ||
---------- | ||
message str | ||
Message to be added to the body of the response | ||
|
||
Returns | ||
------- | ||
Flask.Response | ||
A response object representing the NotImplementedLocally Error | ||
""" | ||
exception_tuple = LambdaErrorResponses.NotImplementedException | ||
|
||
return BaseLocalService.service_response( | ||
LambdaErrorResponses._construct_error_response_body(LambdaErrorResponses.LOCAL_SERVICE_ERROR, message), | ||
LambdaErrorResponses._construct_headers(exception_tuple[0]), | ||
exception_tuple[1] | ||
) | ||
|
||
@staticmethod | ||
def generic_path_not_found(*args): | ||
""" | ||
Creates a Lambda Service Generic PathNotFound Response | ||
|
||
Parameters | ||
---------- | ||
args list | ||
List of arguments Flask passes to the method | ||
|
||
Returns | ||
------- | ||
Flask.Response | ||
A response object representing the GenericPathNotFound Error | ||
""" | ||
exception_tuple = LambdaErrorResponses.PathNotFoundException | ||
|
||
return BaseLocalService.service_response( | ||
LambdaErrorResponses._construct_error_response_body( | ||
LambdaErrorResponses.LOCAL_SERVICE_ERROR, "PathNotFoundException"), | ||
LambdaErrorResponses._construct_headers(exception_tuple[0]), | ||
exception_tuple[1] | ||
) | ||
|
||
@staticmethod | ||
def generic_method_not_allowed(*args): | ||
""" | ||
Creates a Lambda Service Generic MethodNotAllowed Response | ||
|
||
Parameters | ||
---------- | ||
args list | ||
List of arguments Flask passes to the method | ||
|
||
Returns | ||
------- | ||
Flask.Response | ||
A response object representing the GenericMethodNotAllowed Error | ||
""" | ||
exception_tuple = LambdaErrorResponses.MethodNotAllowedException | ||
|
||
return BaseLocalService.service_response( | ||
LambdaErrorResponses._construct_error_response_body(LambdaErrorResponses.LOCAL_SERVICE_ERROR, | ||
"MethodNotAllowedException"), | ||
LambdaErrorResponses._construct_headers(exception_tuple[0]), | ||
exception_tuple[1] | ||
) | ||
|
||
@staticmethod | ||
def _construct_error_response_body(error_type, error_message): | ||
""" | ||
Constructs a string to be used in the body of the Response that conforms | ||
to the structure of the Lambda Service Responses | ||
|
||
Parameters | ||
---------- | ||
error_type str | ||
The type of error | ||
error_message str | ||
Message of the error that occured | ||
|
||
Returns | ||
------- | ||
str | ||
str representing the response body | ||
""" | ||
# OrderedDict is used to make testing in Py2 and Py3 consistent | ||
return json.dumps(OrderedDict([("Type", error_type), ("Message", error_message)])) | ||
|
||
@staticmethod | ||
def _construct_headers(error_type): | ||
""" | ||
Constructs Headers for the Local Lambda Error Response | ||
|
||
Parameters | ||
---------- | ||
error_type str | ||
Error type that occurred to be put into the 'x-amzn-errortype' header | ||
|
||
Returns | ||
------- | ||
dict | ||
Dict representing the Lambda Error Response Headers | ||
""" | ||
return {'x-amzn-errortype': error_type, | ||
'Content-Type': 'application/json'} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume you will fill in the doc strings later
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup. Didn't get that far in life yet :)