- Newest
- Most votes
- Most comments
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
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:
-
In your Catch block, instead of using ResultPath, you'll use the Assign field with JSONata.
-
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.
- 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
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
Relevant content
asked a year ago
- AWS OFFICIALUpdated 3 years ago
