-
Notifications
You must be signed in to change notification settings - Fork 491
Handle error conditions from Lambda function in Runtime API #1953
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
Conversation
@@ -137,16 +153,7 @@ private static APIGatewayHttpApiV2ProxyResponse ToHttpApiV2Response(string respo | |||
catch | |||
{ | |||
// If deserialization fails, return Internal Server Error | |||
return new APIGatewayHttpApiV2ProxyResponse |
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 just put this in its own function so i can re-use it
|
||
if (settings.ApiGatewayEmulatorMode.Equals(ApiGatewayEmulatorMode.HttpV2)) | ||
{ | ||
var lambdaResponse = InvokeResponseExtensions.ToHttpApiV2ErrorResponse(); |
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 kept these function (ToErrorResponse) inside the extensions class because I wasn't sure else where to put them, even though they really arent being used as extensions. I figured it's fine for now but if anyone has better ideas let me know.
i tried for a couple of hours to have unit tests for the api gateway process and lambda runtime api service class, but with the way the current classes are written they are difficult to mock. |
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.
Change looks good. Can we get some tests added to the PR?
@normj i was able to add unit tests for the lambdaruntimeapitests class only. With the way the current ApiGatewayProcess class is written, its not easy to mock/test the stuff that happens in the |
Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Services/LambdaRuntimeAPI.cs
Outdated
Show resolved
Hide resolved
Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Services/LambdaRuntimeAPI.cs
Outdated
Show resolved
Hide resolved
aae407c
to
7a5cc8b
Compare
Issue #, if available: DOTNET-7865
Description of changes:
In this PR, we handle error conditions for when the lambda function itself fails. There are 2 main components.
Handling the error in the lambda runtime api
For handling the error at the lambda runtime api layer we need to mimic how lambda functions behave when they fail. The behavior is described here https://docs.aws.amazon.com/lambda/latest/api/API_Invoke.html#API_Invoke_ResponseSyntax. The TLDR version is that they will set the status code to 200, populate the payload with the
event.ErrorResponse
object and also set theX-Amz-Function-Error
header to theevent.ErrorType
.Handling the lambda error for api gateway
For handling the error at the api gateway layer, it seems that api gateway will just show internal server error when the lambda has an error behind the scenes, so I have done the same.
Testing/Validation
Testing lambda runtime api
In order to verify the accuracy of how i am doing the error handling for the lambda runtime api, did the following. I deployed a lambda function that looks like this (it will throw a null pointer exception and fail)
I then created a console app to invoke the lambda function directly.
I was able to validate that the response code was
200
, the responsePayload contained the sameevnt.ErrorResponse
object that i populate in my code, and that the X-Amz-Function-Error was the same to the one that i populate in my code. (sample fiddler response below)Testing api gateway for handling errors
Internal Server Error
.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.