Skip to content

Convert Step Function Catch ResultPath to Jsonata?

0

Before migrating to Jsonata, our AWS Step Function relied on Catch field's ResultPath in order to include the error as part of the original input, as documented https://docs.aws.amazon.com/step-functions/latest/dg/input-output-resultpath.html#input-output-resultpath-catch:

  • Catch ResultPath Docs

Jsonata has a similar feature here:

  • errorOutput Docs

But how do we use errorOutput so that the Next task called from a Catch contains that error with the original input? I essentially need an equivalency for how Catch ResultPath worked with jsonata.

asked a year ago1.1K views

3 Answers
1
Accepted Answer

I just came across this post while i was looking for an answer. I think i found an easier solution vs using Assign. In the Catch block you can use Output field and jsonata merging. This will merge output to states.input + error and feed this combined object as the Catch "Next" state's input

"Output": "{% $merge([ $states.input, {'error-info': $states.errorOutput} ]) %}"

Refer Jsonata catchers in the ASL specs https://states-language.net/spec.html#fallback-states

answered a year ago

EXPERT

reviewed a year ago

0

To achieve the same functionality as the Catch ResultPath in JSONata for AWS Step Functions, you can use the $states.errorOutput variable along with the $states.input variable. Here's how you can include the error with the original input in the Next task called from a Catch:

  1. In your Catch block, instead of using ResultPath, you'll use the Assign field with JSONata.

  2. Use the following structure in your Assign field:

    "Assign": { "error": "{% $states.errorOutput %}", "input": "{% $states.input %}" }

This will create a new object that includes both the original input and the error information.

  1. In the Next task, you can access this combined information. The error details will be available in the "error" field, and the original input will be in the "input" field.

This approach preserves the original input and includes the error information, similar to how the ResultPath worked in the JSONPath-based approach. The $states.errorOutput variable in JSONata contains the error information when an error occurs, equivalent to the error object in the previous approach.

Remember to set "QueryLanguage": "JSONata" in your state machine definition to use JSONata expressions.

By using this method, you maintain the functionality of including both the error and the original input in your error handling flow, allowing subsequent tasks to access and process this information as needed.
Sources
Specifying state output using ResultPath in Step Functions - AWS Step Functions
Passing data between states with variables - AWS Step Functions

answered a year ago

0

delphicheese gives a readable solution. Here's another way, which I mention because the JSONata transform operator is versatile and consistent with the goal of declarative programming.

Including this:

/* Inside Catch block */
"Output": "{% $states.input ~> |$|$states.errorOutput| %}"

preserves the two keys from $states.errorOutput .

Reference:

/* In next state */
[
   $states.input.Error,  /* Guaranteed */
   $states.input.Cause   /* Optional */
]

There isn't necessarily a need for the extra level of an error-info object.

Either way, be sure to clear the error in that next state, so it won't be passed along to later states:

/* In next state */
"Output": "{% $states.input ~> |$|{}, ['Error', 'Cause']| %}"

To do other work, populate the empty object {} (merges in other data), or chain additional transforms.

Beware the risk of bugs from force of habit! Compare:

[
  $states.errorOutput.Error,      /* Inside Catch block */
  $states.input.Error,            /* In next state */
  $states.input.error-info.Error  /* Or, with delphicheese's solution */
]

Hope this helps!

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.