Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Commit

Permalink
Use different messages on lambda errors (#381)
Browse files Browse the repository at this point in the history
* Update error messages

* Change the message a bit

* Update switch block

* Swap a case condition

* Update tests

* Add a dot
  • Loading branch information
RaeesBhatti authored Mar 7, 2018
1 parent c955490 commit 6049ea7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
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 {
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

0 comments on commit 6049ea7

Please sign in to comment.