Skip to content

Commit c6b2e41

Browse files
smasher164bmoffatt
andauthored
lambda: check if error type is InvokeResponse_Error (#312)
Currently, we cannot set the errorType field without creating a custom named error type. This causes a proliferation of dummy named error types whose only purpose is to set a text field, and limits the errorType field to what can be expressed as an identifier. This change checks that the panicked or returned error is if type InvokeResponse_Error, and if so, uses it directly in the invoker's response. InvokeResponse_Error is now updated to implement error. Note: This is fully backwards compatible, since InvokeResponse_Error previously did not implement the error interface. Fixes #308. Co-authored-by: Bryan Moffatt <[email protected]>
1 parent 4145757 commit c6b2e41

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

lambda/errors.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ func getErrorType(err interface{}) string {
1717
}
1818

1919
func lambdaErrorResponse(invokeError error) *messages.InvokeResponse_Error {
20+
if ive, ok := invokeError.(messages.InvokeResponse_Error); ok {
21+
return &ive
22+
}
2023
var errorName string
2124
if errorType := reflect.TypeOf(invokeError); errorType.Kind() == reflect.Ptr {
2225
errorName = errorType.Elem().Name()
@@ -30,6 +33,9 @@ func lambdaErrorResponse(invokeError error) *messages.InvokeResponse_Error {
3033
}
3134

3235
func lambdaPanicResponse(err interface{}) *messages.InvokeResponse_Error {
36+
if ive, ok := err.(messages.InvokeResponse_Error); ok {
37+
return &ive
38+
}
3339
panicInfo := getPanicInfo(err)
3440
return &messages.InvokeResponse_Error{
3541
Message: panicInfo.Message,

lambda/handler_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"testing"
1010

1111
"github.com/aws/aws-lambda-go/lambda/handlertrace"
12+
"github.com/aws/aws-lambda-go/lambda/messages"
1213
"github.com/stretchr/testify/assert"
1314
)
1415

@@ -187,6 +188,13 @@ func TestInvokes(t *testing.T) {
187188
return struct{ Number int }{event}, nil
188189
},
189190
},
191+
{
192+
input: `"Lambda"`,
193+
expected: expected{"", messages.InvokeResponse_Error{Message: "message", Type: "type"}},
194+
handler: func(e interface{}) (interface{}, error) {
195+
return nil, messages.InvokeResponse_Error{Message: "message", Type: "type"}
196+
},
197+
},
190198
}
191199
for i, testCase := range testCases {
192200
testCase := testCase

lambda/messages/messages.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
package messages
44

5+
import "fmt"
6+
57
type PingRequest struct {
68
}
79

@@ -36,6 +38,10 @@ type InvokeResponse_Error struct {
3638
ShouldExit bool `json:"-"`
3739
}
3840

41+
func (e InvokeResponse_Error) Error() string {
42+
return fmt.Sprintf("%#v", e)
43+
}
44+
3945
type InvokeResponse_Error_StackFrame struct {
4046
Path string `json:"path"`
4147
Line int32 `json:"line"`

lambda/panic_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77
"testing"
88

9+
"github.com/aws/aws-lambda-go/lambda/messages"
910
"github.com/stretchr/testify/assert"
1011
)
1112

@@ -37,6 +38,11 @@ func TestPanicFormattingCustomError(t *testing.T) {
3738
assertPanicMessage(t, func() { panic(customError) }, customError.Error())
3839
}
3940

41+
func TestPanicFormattingInvokeResponse_Error(t *testing.T) {
42+
ive := &messages.InvokeResponse_Error{Message: "message", Type: "type"}
43+
assertPanicMessage(t, func() { panic(ive) }, ive.Error())
44+
}
45+
4046
func TestFormatFrame(t *testing.T) {
4147
var tests = []struct {
4248
inputPath string

0 commit comments

Comments
 (0)