I am trying to add error handling in my step function flow using the Parallel and Catch blocks as defined in the State Machine Language.
Following is the flow diagram of my step functions:
Since I want a common error handler for all the step functions, I have wrapped them in a Parallel block and added a common Catch block to catch any errors in any of the step functions. On looking through various examples and blogs, I followed this link and implemented a similar approach.
What I observe is that, whenever any state raises an exception, the control goes into the catch block. The input to the catch block is the exception that was raised containing an Error and Cause in a JSON object. Since I wanted the error along with the input that was passed to that state, I added the ResultPath as "$.error" in the catch block. Following is the JSON spec that defines the state machine.
{
"StartAt": "Try",
"States": {
"Try": {
"Type": "Parallel",
"Branches": [
{
"StartAt": "Step-1",
"States": {
"Step-1": {
"Type": "Task",
"Resource": "arn:aws:lambda:eu-west-1:1234:function:step-1-lambda",
"Next": "Step-2"
},
"Step-2": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.some_variable",
"StringEquals": "some_string",
"Next": "Step-3"
},
{
"Variable": "$.some_variable",
"StringEquals": "some_other_string",
"Next": "Step-4"
}
],
"Default": "Step-6"
},
"Step-3": {
"Type": "Task",
"Resource": "arn:aws:lambda:eu-west-1:1234:function:step-3-lambda",
"Next": "Step-6"
},
"Step-4": {
"Type": "Task",
"Resource": "arn:aws:lambda:eu-west-1:1234:function:step-4-lambda",
"Next": "Step-6"
},
"Step-6": {
"Type": "Task",
"Resource": "arn:aws:lambda:eu-west-1:1234:function:step-6-lambda",
"End": true
}
}
}
],
"Catch": [
{
"ErrorEquals": [
"States.ALL"
],
"ResultPath": "$.error",
"Next": "ErrorHandler"
}
],
"Next": "UnwrapOutput"
},
"UnwrapOutput": {
"Type": "Pass",
"InputPath": "$[0]",
"End": true
},
"ErrorHandler": {
"Type": "Task",
"Resource": "arn:aws:lambda:eu-west-1:1234:function:step-7-lambda",
"End": true
}
}
}
For example, consider that Step-4 generates an exception. The input to this state is:
{
"foo": "abc",
"bar": "def"
}
The input with which the state machine is triggered is:
{
"boo": "jkl",
"baz": "mno"
}
In the ErrorHandler, as Step-4 generates an exception I was expecting that the input to the ErrorHandler state would be:
{
"foo": "abc",
"bar": "def",
"error": {
"Error": "SomeError",
"Cause": "SomeCause"
}
}
However, the input received consists of the original input that is used to trigger the flow.
{
"boo": "jkl",
"baz": "mno",
"error": {
"Error": "SomeError",
"Cause": "SomeCause"
}
}
I need to access the input fields of the state that caused the exception in the ErrorHandler. Using "$" it provides the input that was used to trigger the flow. Is there a way I can achieve this?
Any help would be appreciated, I am trying to figure this out since a long time.