Last active
April 5, 2023 19:47
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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