Skip to content

Commit

Permalink
change internal function error name (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
mthenw authored Aug 15, 2017
1 parent dec7b02 commit 28293c6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
15 changes: 12 additions & 3 deletions functions/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,20 @@ func (e ErrFunctionCallFailed) Error() string {
return fmt.Sprintf("Function call failed. Error: %q", e.original)
}

// ErrFunctionCallFailedProviderError occurs when function call failed because of provider error.
type ErrFunctionCallFailedProviderError struct {
// ErrFunctionProviderError occurs when function call failed because of provider error.
type ErrFunctionProviderError struct {
original error
}

func (e ErrFunctionCallFailedProviderError) Error() string {
func (e ErrFunctionProviderError) Error() string {
return fmt.Sprintf("Function call failed because of provider error. Error: %q", e.original)
}

// ErrFunctionError occurs when function call failed because of function error.
type ErrFunctionError struct {
original error
}

func (e ErrFunctionError) Error() string {
return fmt.Sprintf("Function call failed because of runtime error. Error: %q", e.original)
}
12 changes: 8 additions & 4 deletions functions/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,17 @@ func (f *Function) callAWSLambda(payload []byte) ([]byte, error) {
if awserr, ok := err.(awserr.Error); ok {
switch awserr.Code() {
case lambda.ErrCodeServiceException:
return nil, &ErrFunctionCallFailedProviderError{awserr}
return nil, &ErrFunctionProviderError{awserr}
default:
return nil, &ErrFunctionCallFailed{awserr}
}
}
}

if invokeOutput.FunctionError != nil {
return nil, &ErrFunctionError{errors.New(*invokeOutput.FunctionError)}
}

return invokeOutput.Payload, err
}

Expand All @@ -161,7 +165,7 @@ func (f *Function) callHTTP(payload []byte) ([]byte, error) {
return nil, &ErrFunctionCallFailed{err}
}
if resp.StatusCode == http.StatusInternalServerError {
return nil, &ErrFunctionCallFailedProviderError{fmt.Errorf("HTTP status code: %d", http.StatusInternalServerError)}
return nil, &ErrFunctionError{fmt.Errorf("HTTP status code: %d", http.StatusInternalServerError)}
}

defer resp.Body.Close()
Expand All @@ -170,7 +174,7 @@ func (f *Function) callHTTP(payload []byte) ([]byte, error) {

func (f *Function) callEmulator(payload []byte) ([]byte, error) {
type emulatorInvokeSchema struct {
FunctionID string `json:"functionId"`
FunctionID string `json:"functionId"`
Payload interface{} `json:"payload"`
}

Expand Down Expand Up @@ -206,7 +210,7 @@ func (f *Function) callEmulator(payload []byte) ([]byte, error) {
return nil, &ErrFunctionCallFailed{err}
}
if resp.StatusCode == http.StatusInternalServerError {
return nil, &ErrFunctionCallFailedProviderError{fmt.Errorf("HTTP status code: %d", http.StatusInternalServerError)}
return nil, &ErrFunctionError{fmt.Errorf("HTTP status code: %d", http.StatusInternalServerError)}
}

defer resp.Body.Close()
Expand Down
24 changes: 8 additions & 16 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const (
// headerFunctionID is a header name for specifing function id for sync invocation.
headerFunctionID = "function-id"

internalFunctionProviderError = "gateway.warn.functionProviderError"
internalFunctionError = "gateway.info.functionError"
)

var (
Expand Down Expand Up @@ -189,15 +189,15 @@ func (router *Router) handleSyncEvent(name string, payload []byte, w http.Respon

resp, err := router.callFunction(functionID, payload)
if err != nil {
router.log.Warn("Function invocation failed.",
router.log.Info("Function invocation failed.",
zap.String("functionId", string(functionID)), zap.String("event", string(payload)), zap.Error(err))

if err == errUnableToLookUpBackingFunction {
http.Error(w, err.Error(), http.StatusNotFound)
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)

router.emitFunctionProviderErrorEvent(functionID, payload, err)
router.emitFunctionErrorEvent(functionID, payload, err)
}

return
Expand Down Expand Up @@ -315,18 +315,10 @@ func (router *Router) processEvent(e event) {
resp, err := router.callFunction(subscriber, e.payload)

if err != nil {
router.log.Warn("Function invocation failed.",
router.log.Info("Function invocation failed.",
zap.String("functionId", string(subscriber)), zap.String("event", string(e.payload)), zap.Error(err))

if _, ok := err.(*functions.ErrFunctionCallFailedProviderError); ok {
internal := NewEvent(internalFunctionProviderError, mimeJSON, struct {
FunctionID string `json:"functionId"`
}{string(subscriber)})
payload, err := json.Marshal(internal)
if err == nil {
router.enqueueWork(subscriptions.TopicID(internal.Event), payload)
}
}
router.emitFunctionErrorEvent(subscriber, e.payload, err)
} else {
router.log.Debug("Function finished.",
zap.String("functionId", string(subscriber)), zap.String("event", string(e.payload)),
Expand All @@ -335,9 +327,9 @@ func (router *Router) processEvent(e event) {
}
}

func (router *Router) emitFunctionProviderErrorEvent(functionID functions.FunctionID, payload []byte, err error) {
if _, ok := err.(*functions.ErrFunctionCallFailedProviderError); ok {
internal := NewEvent(internalFunctionProviderError, mimeJSON, struct {
func (router *Router) emitFunctionErrorEvent(functionID functions.FunctionID, payload []byte, err error) {
if _, ok := err.(*functions.ErrFunctionError); ok {
internal := NewEvent(internalFunctionError, mimeJSON, struct {
FunctionID string `json:"functionId"`
}{string(functionID)})
payload, err = json.Marshal(internal)
Expand Down

0 comments on commit 28293c6

Please sign in to comment.