Skip to content

Commit

Permalink
resolves apex#59 - enables go functions to specify the errorType to s…
Browse files Browse the repository at this point in the history
…upport AWS Step Function retries

See bottom of:

http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-mode-exceptions.html
  • Loading branch information
savaki committed Oct 26, 2017
1 parent 338ee01 commit 3f1bbe2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# ignore intellij files
#
.idea
*.iml
*.ipr
*.iws
60 changes: 58 additions & 2 deletions apex.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package apex

import (
"encoding/json"
"fmt"
"io"
"log"
"os"
Expand Down Expand Up @@ -73,7 +74,7 @@ type input struct {
type output struct {
// The boomeranged ID from the caller
ID string `json:"id,omitempty"`
Error string `json:"error,omitempty"`
Error interface{} `json:"error,omitempty"`
Value interface{} `json:"value,omitempty"`
}

Expand All @@ -84,6 +85,50 @@ type manager struct {
Handler Handler
}

// Error allows apex functions to return structured node errors
// http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-mode-exceptions.html
//
// Unfortunately, AWS doesn't provide a mechanism to override the stack trace.
// Consequently, when a custom error is returned the node stack trace will be
// used which isn't particularly helpful.
//
type Error interface {
// Error implements error
Error() string

// ErrorType provides a specific error type useful for switching in Step Functions
ErrorType() string

// ErrorMessage contains the human readable string message
ErrorMessage() string
}

// customError holds custom error message
type customError struct {
errorType string
errorMessage string
}

func (c customError) Error() string {
return fmt.Sprintf("%v: %v", c.errorType, c.errorMessage)
}

func (c customError) ErrorType() string {
return c.errorType
}

func (c customError) ErrorMessage() string {
return c.errorMessage
}

// NewError creates Error message with custom error type; useful for AWS Step Functions
func NewError(errorType, errorMessage string) Error {
return customError{
errorType: errorType,
errorMessage: errorMessage,
}
}

// Start the manager.
func (m *manager) Start() {
dec := json.NewDecoder(m.Reader)
Expand All @@ -106,7 +151,18 @@ func (m *manager) Start() {
out := output{ID: msg.ID, Value: v}

if err != nil {
out.Error = err.Error()
if ae, ok := err.(Error); ok {
out.Error = struct {
ErrorType string `json:"errorType,omitempty"`
ErrorMessage string `json:"errorMessage,omitempty"`
}{
ErrorType: ae.ErrorType(),
ErrorMessage: ae.ErrorMessage(),
}

} else {
out.Error = err.Error()
}
}

if err := enc.Encode(out); err != nil {
Expand Down

0 comments on commit 3f1bbe2

Please sign in to comment.