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

resolves #59 - enables go functions to specify the errorType to support AWS Step Function retries #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 .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