Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
fix(stepfunctions): apply a JSONata catcher's Output and Assign on Map and Parallel states (#2424)
mishukdutta-cz1e9c42ae3d55
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 02615a75..85ed3a93 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).## [Unreleased]### Fixed+- **Step Functions — JSONata `Catch` on `Map` and `Parallel` states** — a catcher's `Output` and `Assign` were ignored, so the caught error passed on unchanged and the catcher's variables were never set; both now apply, as on a `Task` state.- **Step Functions — JSONata variables with the value `null`** — a variable assigned `null` read back as undefined, so `{% $var %}` failed with `States.QueryEvaluationError`; it now reads back as `null`, as on AWS. An unset variable still fails.- **Lambda — Kinesis event source mapping filters** — a `FilterCriteria` pattern on a Kinesis record's `data` or on its metadata (`partitionKey`, `sequenceNumber`, ...) never matched, so a mapping with filters dropped every record. Patterns are now matched as AWS documents them: metadata fields next to the event fields, and `data` decoded from base64 as JSON. Data that isn't JSON can't match a `data` pattern but still matches metadata patterns.- **Lambda — two MiniStacks on one host no longer remove each other's function code** — the provided-runtime and Docker-executor extraction caches lived at fixed paths in the shared temp directory, so a second instance on the same host deleted a tree the first was still running from ("leftover from a previous process") and its `reset()` removed the first one's caches; an invocation then failed with `No such file or directory: .../bootstrap`. Both caches are now named for the instance's `GATEWAY_PORT`. Contributed by @skialpine.diff --git a/ministack/services/stepfunctions.py b/ministack/services/stepfunctions.pyindex 23d86e36..9b784713 100644--- a/ministack/services/stepfunctions.py+++ b/ministack/services/stepfunctions.py@@ -2052,21 +2052,7 @@ def _execute_task(state_def, raw_input, execution, ctx):catcher = _find_matching_catcher(catchers, last_error.error)if catcher:error_output = {"Error": last_error.error, "Cause": last_error.cause}- if query_language == "JSONata" and "Output" in catcher:- output = _apply_jsonata_output(- catcher,- raw_input,- ctx,- error_output=error_output,- default=error_output,- )- else:- output = _apply_result_path_raw(- catcher.get("ResultPath", "$"), raw_input, error_output)- if query_language == "JSONata":- # Catch handlers can also carry `Assign`. `$states.errorOutput`- # is available here; `$states.result` is not (per AWS).- _apply_state_assign(catcher, raw_input, ctx, error_output=error_output)+ output = _apply_catcher(catcher, raw_input, ctx, query_language, error_output)return output, catcher["Next"]raise last_error@@ -2565,7 +2551,7 @@ def _with_retry_and_catch(state_def, raw_input, ctx, attempt):if catcher is None:raiseerror_output = {"Error": err.error, "Cause": err.cause}- output = _apply_result_path_raw(catcher.get("ResultPath", "$"), raw_input, error_output)+ output = _apply_catcher(catcher, raw_input, ctx, _state_query_language(state_def, ctx), error_output)return output, catcher["Next"]@@ -3653,6 +3639,19 @@ def _find_matching_catcher(catchers, error):return None+def _apply_catcher(catcher, raw_input, ctx, query_language, error_output):+ """Build a caught error's output and apply the catcher's Assign."""+ if query_language == "JSONata" and "Output" in catcher:+ output = _apply_jsonata_output(catcher, raw_input, ctx, error_output=error_output, default=error_output)+ else:+ output = _apply_result_path_raw(catcher.get("ResultPath", "$"), raw_input, error_output)+ if query_language == "JSONata":+ # Catch handlers can also carry `Assign`. `$states.errorOutput`+ # is available here; `$states.result` is not (per AWS).+ _apply_state_assign(catcher, raw_input, ctx, error_output=error_output)+ return output++# ===================================================================# Misc helpers# ===================================================================diff --git a/tests/test_stepfunctions.py b/tests/test_stepfunctions.pyindex 45db5863..452c248a 100644--- a/tests/test_stepfunctions.py+++ b/tests/test_stepfunctions.py@@ -3696,6 +3696,41 @@ def test_sfn_parallel_and_map_apply_retry_and_catch(sfn, state):assert json.loads(desc["output"]) == {"xs": [1], "err": {"Error": "MyErr", "Cause": "c"}}[email protected]("state", [+ {"Type": "Parallel", "Branches": [_SFN_FAIL_BRANCH]},+ {"Type": "Map", "ItemProcessor": _SFN_FAIL_BRANCH},+], ids=["parallel", "map"])+def test_sfn_jsonata_parallel_and_map_catch_apply_output_and_assign(sfn, state):+ """A JSONata catcher on a Parallel or Map state applies its Output, and its Assign sets variables in the+ outer scope."""+ definition = json.dumps({+ "QueryLanguage": "JSONata",+ "StartAt": "S",+ "States": {+ "S": {+ **state,+ "Catch": [{+ "ErrorEquals": ["MyErr"],+ "Output": {"caught": "{% $states.errorOutput.Error %}"},+ "Assign": {"err": "{% $states.errorOutput %}"},+ "Next": "Caught",+ }],+ "End": True,+ },+ "Caught": {"Type": "Pass", "Output": {"output": "{% $states.input %}", "err": "{% $err %}"}, "End": True},+ },+ })+ sm = sfn.create_state_machine(+ name=f"sfn-jsonata-{state['Type'].lower()}-catch-{_uuid_mod.uuid4().hex[:8]}", definition=definition,+ roleArn="arn:aws:iam::000000000000:role/R",+ )+ ex = sfn.start_execution(stateMachineArn=sm["stateMachineArn"], input="[1]")++ desc = _wait_sfn(sfn, ex["executionArn"])+ assert desc["status"] == "SUCCEEDED", desc.get("cause")+ assert json.loads(desc["output"]) == {"output": {"caught": "MyErr"}, "err": {"Error": "MyErr", "Cause": "c"}}++def test_sfn_integration_lambda_invoke_wait_for_task_token(sfn, lam):"""lambda:invoke.waitForTaskToken must deliver the *unwrapped* Payload tothe handler, exactly like the synchronous lambda:invoke path.