aws / aws-lambda-go

Libraries, samples and tools to help Go developers develop AWS Lambda functions.
Apache License 2.0
3.64k stars 553 forks source link

Allow Error return customization #433

Open driverpt opened 2 years ago

driverpt commented 2 years ago

Is your feature request related to a problem? Please describe. We have the need to customize Error responses, e.g.: Add an Error Code/Status Code. And we feel that it's kind of limiting to only have messages.InvokeResponse_Error{} as Error Return.

Describe the solution you'd like Is it possible to make this customizable via Custom JSON Serialization ?

Describe alternatives you've considered N/A

Additional context N/A

bmoffatt commented 2 years ago

integrations that call Lambdas have had years to ossify the assumption that an error json will look something like this:

{
...
  "errorMessage": ...,
...
}

so the approach for this probably shouldn't be as simple as delegating json.Marshal, that feels like a footgun for a bunch of users if errorMessage could accidentally go missing. Somehow the messages.InvokeResponse_Error{} and your custom serialization result would have to be merged. Or perhaps the custom serialization result nested inside like:

type InvokeResponse_Error struct {
    Message    string                             `json:"errorMessage"`
    Type       string                             `json:"errorType"`
    StackTrace []*InvokeResponse_Error_StackFrame `json:"stackTrace,omitempty"`
    ShouldExit bool                               `json:"-"`
    // new field, populated with the result of `json.Marshal` if the handler error has a `MarshalJSON` receiver defined
    Custom json.RawMessage                        `json:"custom,omitempty"`
}

/cc @carlzogh

If we went this this approach, we'd also wanna check that it behaves with the go1.x RPC client.


@driverpt Are you running your functions with go1.x, provided, or provided.al2? If your custom fields appeared nested in the existing error json, would that still fit your use case?

driverpt commented 2 years ago

Our functions are running on provided.al2. Ideally it would have a json.Marshall(err).

Is it possible to have a Global Flag to tell the handler to use Custom errors?