aws / aws/aws-lambda-go

Custom error response payload

Open
#341 1 comment 4 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
3.8k
Forks
578
Avg merge
8h 18m
Merged PRs (30d)
1

Description

**Is your feature request related to a problem? Please describe.**
I am frustrated (your terminology) that I cannot customize the error response payload in a lambda.

I'm using a transport agnostic API ([GraphQL](https://www.howtographql.com/advanced/5-common-questions/)) inside of my Lambda function. Per the GraphQL specification, the response contains `{"data": {...stuff}}` when all was good, or `{"data": {...stuff}, "errors": [... stuff]}` when there are errors. `return graphqlPayload, nil` works great in most scenarios: I can easily grant permissions to those who should have access, they can easily run the function and parse the response, check for errors, etc.

`return graphqlPayload, nil` does not work well when running a step function. Specifically, I need to implement my own retry loop to check for `$.output.Payload.errors`. -- Yuk

**Describe the solution you'd like**
I would like to be able to do something [like this](https://play.golang.org/p/USUbHj1PRw9):

```go
if returnError {
return nil, aws-lambda-go/lambda/messages.WrapError(graphqlPayload)
} else {
return graphqlPayload, nil
}
```

**Describe alternatives you've considered**
* I've considered using API gateway, but that adds a lot of complexity when compared to simply invoking a Lambda where the permissions are very easily controlled/managed through CFT/CDK. Also, I don't think [I was able to use API Gateway](https://aws.amazon.com/about-aws/whats-new/2020/11/aws-step-functions-supports-amazon-api-gateway-service-integration/) within a step function. Plus, I think I'd still need to figure out HTTP error codes for something that is supposed to be transport agnostic.
* I've considered `return payload, someError`, but [the payload is discarded](https://github.com/aws/aws-lambda-go/blob/406b8fc429988d9ee04afc770794e79ed44a1964/lambda/function.go#L69) when there is an error.
* I've considered `return nil, someError`, but [the error I provide is discarded](https://github.com/aws/aws-lambda-go/blob/406b8fc429988d9ee04afc770794e79ed44a1964/lambda/errors.go#L20-L32)
* I've considered creating a second lambda function to invoke in step functions. This is what I'll have to do. it makes me sad.

**Additional context**
* I've read this [related issue](https://github.com/aws/aws-lambda-go/issues/308) and [https://github.com/aws/aws-lambda-go/pull/312](PR). I see that you mention going to `v2`, but that may not be necessary if you were to provide a `WrapError` method.
* I've also seen the open issue [Examples shouldn't suggest to return error objects](https://github.com/aws/aws-lambda-go/issues/272), which makes it seem as though this is just a documentation issue.

Could something [like this MWE goplay](https://play.golang.org/p/USUbHj1PRw9) work? Is there a requirement elsewhere that the payload of an error contain `errorMessage` and `errorType`?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.