Skip to content
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

lambda: check if error type is InvokeResponse_Error #312

Merged
merged 2 commits into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lambda/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ func getErrorType(err interface{}) string {
}

func lambdaErrorResponse(invokeError error) *messages.InvokeResponse_Error {
if ive, ok := invokeError.(messages.InvokeResponse_Error); ok {
return &ive
}
var errorName string
if errorType := reflect.TypeOf(invokeError); errorType.Kind() == reflect.Ptr {
errorName = errorType.Elem().Name()
Expand All @@ -30,6 +33,9 @@ func lambdaErrorResponse(invokeError error) *messages.InvokeResponse_Error {
}

func lambdaPanicResponse(err interface{}) *messages.InvokeResponse_Error {
if ive, ok := err.(messages.InvokeResponse_Error); ok {
return &ive
}
panicInfo := getPanicInfo(err)
return &messages.InvokeResponse_Error{
Message: panicInfo.Message,
Expand Down
8 changes: 8 additions & 0 deletions lambda/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"github.com/aws/aws-lambda-go/lambda/handlertrace"
"github.com/aws/aws-lambda-go/lambda/messages"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -187,6 +188,13 @@ func TestInvokes(t *testing.T) {
return struct{ Number int }{event}, nil
},
},
{
input: `"Lambda"`,
expected: expected{"", messages.InvokeResponse_Error{Message: "message", Type: "type"}},
handler: func(e interface{}) (interface{}, error) {
return nil, messages.InvokeResponse_Error{Message: "message", Type: "type"}
},
},
}
for i, testCase := range testCases {
testCase := testCase
Expand Down
6 changes: 6 additions & 0 deletions lambda/messages/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

package messages

import "fmt"

type PingRequest struct {
}

Expand Down Expand Up @@ -36,6 +38,10 @@ type InvokeResponse_Error struct {
ShouldExit bool `json:"-"`
}

func (e InvokeResponse_Error) Error() string {
return fmt.Sprintf("%#v", e)
}

type InvokeResponse_Error_StackFrame struct {
Path string `json:"path"`
Line int32 `json:"line"`
Expand Down
6 changes: 6 additions & 0 deletions lambda/panic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"testing"

"github.com/aws/aws-lambda-go/lambda/messages"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -37,6 +38,11 @@ func TestPanicFormattingCustomError(t *testing.T) {
assertPanicMessage(t, func() { panic(customError) }, customError.Error())
}

func TestPanicFormattingInvokeResponse_Error(t *testing.T) {
ive := &messages.InvokeResponse_Error{Message: "message", Type: "type"}
assertPanicMessage(t, func() { panic(ive) }, ive.Error())
}

func TestFormatFrame(t *testing.T) {
var tests = []struct {
inputPath string
Expand Down