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

Use different messages on lambda errors #381

Merged
merged 6 commits into from
Mar 7, 2018
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: 3 additions & 3 deletions function/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ func (f *Function) callAWSLambda(payload []byte) ([]byte, error) {
if err != nil {
if awserr, ok := err.(awserr.Error); ok {
switch awserr.Code() {
case "AccessDeniedException":
case "InvalidSignatureException":
case "UnrecognizedClientException":
case "AccessDeniedException",
"ExpiredTokenException",
"UnrecognizedClientException":
return nil, &ErrFunctionAccessDenied{awserr}
case lambda.ErrCodeServiceException:
return nil, &ErrFunctionProviderError{awserr}
Expand Down
32 changes: 27 additions & 5 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/serverless/event-gateway/function"
"github.com/serverless/event-gateway/httpapi"
"github.com/serverless/event-gateway/plugin"
"github.com/aws/aws-sdk-go/aws/awserr"
)

// Router calls a target function when an endpoint is hit, and handles pubsub message delivery.
Expand Down Expand Up @@ -238,10 +239,7 @@ func (router *Router) handleHTTPEvent(event *eventpkg.Event, w http.ResponseWrit
event.Data = httpdata
resp, err := router.callFunction(space, *backingFunction, *event)
if err != nil {
message := "function call failed"
if _, ok := err.(*function.ErrFunctionAccessDenied); ok {
message = "function access denied"
}
message := determineErrorMessage(err)

w.WriteHeader(http.StatusInternalServerError)
w.Header().Set("Content-Type", "application/json")
Expand Down Expand Up @@ -305,7 +303,8 @@ func (router *Router) handleInvokeEvent(space string, functionID function.ID, pa
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Header().Set("Content-Type", "application/json")
encoder.Encode(&httpapi.Response{Errors: []httpapi.Error{{Message: err.Error()}}})
message := determineErrorMessage(err)
encoder.Encode(&httpapi.Response{Errors: []httpapi.Error{{Message: message}}})
return
}

Expand All @@ -318,6 +317,29 @@ func (router *Router) handleInvokeEvent(space string, functionID function.ID, pa
}
}

func determineErrorMessage(err error) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should not be defined in router package. It should be defined in function package. This is where providers specific logic should live.

message := "Function call failed. Please check logs."
if accessError, ok := err.(*function.ErrFunctionAccessDenied); ok {
if originalErr, ok := accessError.Original.(awserr.Error); ok {
switch originalErr.Code() {
case "AccessDeniedException":
message = "Function call failed with AccessDeniedException. The provided credentials do not" +
" have the required IAM permissions to invoke this function. Please attach the" +
" lambda:invokeFunction permission to these credentials."
case "UnrecognizedClientException":
message = "Function call failed with UnrecognizedClientException. The provided credentials" +
" are invalid. Please provide valid credentials."
case "ExpiredTokenException":
message = "Function call failed with ExpiredTokenException. The provided security token for" +
" the function has expired. Please provide an updated security token or provide" +
" permanent credentials."
}
}
}

return message
}

func (router *Router) enqueueWork(path string, event *eventpkg.Event) {
if event.IsSystem() {
router.log.Debug("System event received.", zap.Object("event", event))
Expand Down
2 changes: 1 addition & 1 deletion router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestRouterServeHTTP_InvokeEventFunctionNotFound(t *testing.T) {
router.ServeHTTP(recorder, req)

assert.Equal(t, http.StatusInternalServerError, recorder.Code)
assert.Equal(t, `{"errors":[{"message":"unable to look up registered function"}]}`+"\n", recorder.Body.String())
assert.Equal(t, `{"errors":[{"message":"Function call failed. Please check logs."}]}`+"\n", recorder.Body.String())
}

func TestRouterServeHTTP_InvokeEventDefaultSpace(t *testing.T) {
Expand Down