Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data to step function catch

I'm using step functions on AWS. Consider the state machine made up of lambdas:

"StartAt": "Metadata",
              "States": {
                    "Metadata": {
                    "Type": "Task",
                    "Resource": "${metadataArn}",
                    "Next": "StoreMetadata",
                    "Retry" : [
                            {
                                "ErrorEquals": [ "States.All" ],
                                "IntervalSeconds": 2,
                                "MaxAttempts": 3
                            }
                    ],
                    "Catch": [
                        {
                            "ErrorEquals": [ "States.All" ],
                            "Next": "ErrorHandler"
                        }
                    ]
                  } ...
                      ...

How can I pass particular data to the "ErrorHandler". For example, the step that failed, maybe a piece of data. I'm using nodejs but can extrapolate to any runtime.

For example in node we might have a lambda where:

module.exports.handler = async input => {
  const ids = JSON.parse(input).ids
  // try to read in data for ids.
  // read fails / throws exception
}

How can I have the error handler get the array of ids so I can mark them as failed? if this "ErrorHandler" is the catch for multiple steps how can I have it know which steps failed?

like image 995
Zachscs Avatar asked Mar 12 '19 19:03

Zachscs


People also ask

What is ResultPath in step function?

Use ResultPath to combine a task result with task input, or to select one of these. The path you provide to ResultPath controls what information passes to the output. ResultPath is limited to using reference paths, which limit scope so that it can identify only a single node in JSON.


2 Answers

I found an answer, you can use ResultPath to pass the original input along with the error. I suppose I will include the step as a property in all inputs so that I can know what step failed. See the docs for an explanation. Essentially to accomplish this you would just add the ResultPath property like so:

"Catch": [
  {
    "ErrorEquals": [ "States.All" ],
    "Next": "ErrorHandler"
    "ResultPath": "$.error"
  }
]
like image 135
Zachscs Avatar answered Oct 05 '22 03:10

Zachscs


I wanted to add to @Zachscs answer that you need to be careful when doing "Catch" on "Type": "Map", as this is and doing "ResultPath": "$.error", will throw:

Unable to apply step "error" to input [...]

This makes sense, as the input is an array. You solve it by adding a simple zero-based index to error like this:

"Type": "Map",
"Next": "Finish",
"Catch": [
  {
    "ErrorEquals": [ "States.All" ],
    "Next": "ErrorHandler",
    "Comment": "Note the $[0] down below",
    "ResultPath": "$[0].error"
  }
]

This will append it to the second index of the array $[1].error

like image 43
Jose A Avatar answered Oct 05 '22 01:10

Jose A