From 3f1bbe29802d90fe8fe8b65e936b21409218a586 Mon Sep 17 00:00:00 2001 From: savaki Date: Thu, 26 Oct 2017 09:51:35 -0700 Subject: [PATCH] resolves #59 - enables go functions to specify the errorType to support AWS Step Function retries See bottom of: http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-mode-exceptions.html --- .gitignore | 6 ++++++ apex.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e866a07 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +# ignore intellij files +# +.idea +*.iml +*.ipr +*.iws diff --git a/apex.go b/apex.go index 2365849..340ebe5 100644 --- a/apex.go +++ b/apex.go @@ -5,6 +5,7 @@ package apex import ( "encoding/json" + "fmt" "io" "log" "os" @@ -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"` } @@ -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) @@ -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 {