Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@massenz
Last active April 5, 2023 19:47
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save massenz/4dcaa7046c433ab529ed948e7db302d9 to your computer and use it in GitHub Desktop.
Save massenz/4dcaa7046c433ab529ed948e7db302d9 to your computer and use it in GitHub Desktop.
Demonstrates how to generate different types of errors for AWS Lambda Step Functions, so that Retry/Catch can distinguish between different kinds of errors.
package main
import (
"context"
"fmt"
"github.com/aws/aws-lambda-go/lambda"
"log"
)
func FailHander(ctx context.Context, req ErrorRequest) error {
if req.Outcome == "base" {
return fmt.Errorf("bog-standard error")
} else if req.Outcome == "fancy" {
return &HandlerError{"error from a failed handler"}
} else {
log.Println("This was good, really good :)")
return nil
}
}
type HandlerError struct {
Detail string `json:"detail"`
}
func (e *HandlerError) Error() string {
return e.Detail
}
type ErrorRequest struct {
Outcome string `json:"outcome"`
}
func main() {
lambda.Start(FailHander)
}
{
"Comment": "A state machine to experiment with errors",
"StartAt": "Error Handler",
"States": {
"Error Handler": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"OutputPath": "$.Payload",
"Parameters": {
"Payload.$": "$",
"FunctionName": "arn:aws:<AWS REGION>:<YOUR AWS ACCOUNT ID HERE>:function:book-error:$LATEST"
},
"Catch": [
{
"ErrorEquals": [
"errorString"
],
"Next": "Error String"
},
{
"ErrorEquals": [
"HandlerError"
],
"Next": "Handler Error"
}
],
"End": true
},
"Handler Error": {
"Type": "Fail"
},
"Error String": {
"Type": "Succeed"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment