feat(stepfunctions): resolve Fail state ErrorPath and CausePath dynamically (#3302)

A Fail state now resolves Error and Cause dynamically from ErrorPath and
CausePath, evaluated against the state input through the same JSONPath
resolver a payload template field uses. CreateStateMachine now also rejects
a Fail state that declares both Error and ErrorPath, or both Cause and
CausePath, as AWS does, and rejects a non-string ErrorPath/CausePath value
at definition time rather than letting it reach execution and fail with
States.Runtime.

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>

* Fix #3255
This commit is contained in:
Matheus Oliveira authored and GitHub committed 2026-09-09 23:31:05 -05:00
1 parent d44a3fef62
commit 650914f5f1
4 files changed
+392 -1

No files matched your search

@@ -2039,11 +2039,33 @@ public class AslExecutor {
if (cause != null && JsonataEvaluator.isExpression(cause)) {
cause = jsonataEvaluator.evaluateField(cause, "Cause", statesVar, variables).asText();
}
} else {
if (stateDef.has("ErrorPath")) {
error = resolveFailDynamicField(stateDef.get("ErrorPath").asText(), "ErrorPath", input, context);
}
if (stateDef.has("CausePath")) {
cause = resolveFailDynamicField(stateDef.get("CausePath").asText(), "CausePath", input, context);
}
}
// AWS does not prefix a Fail state's Cause.
throw new FailStateException(error, cause, true);
}
/**
* Resolves a Fail state's {@code ErrorPath} or {@code CausePath}: a reference path or a
* {@code States.*} intrinsic, evaluated against the state's input through the same resolver a
* {@code ".$"} payload template field uses. AWS requires the resolved value to be a string;
* an unresolvable path or a non-string result both fail the state with {@code States.Runtime},
* since a Fail state has no {@code Catch} of its own to route around either failure.
*/
private String resolveFailDynamicField(String path, String field, JsonNode input, JsonNode context) {
JsonNode resolved = resolvePayloadTemplateReference(path, input, context, field, input);
if (!resolved.isTextual()) {
throw new FailStateException("States.Runtime", field + " must resolve to a string");
}
return resolved.asText();
}
private StateResult executeParallelState(String name, JsonNode stateDef, JsonNode input,
HistoryChain chain, StateMachine sm, boolean jsonata,
String topLevelQueryLanguage, JsonNode context,
@@ -72,7 +72,7 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
// AWS-observed, it is simply the one this code commits to.
private static final List<String> JSONPATH_ONLY_FIELDS = List.of(
"InputPath", "OutputPath", "ResultPath", "ResultSelector", "Parameters", "Result", "ItemsPath",
"MaxConcurrencyPath");
"MaxConcurrencyPath", "ErrorPath", "CausePath");
// Fields that are valid only in JSONata mode. Validated against real AWS: a JSONPath state
// carrying any of them returns SCHEMA_VALIDATION_FAILED. Assign is deliberately absent: AWS
// accepts it on a JSONPath state, so it belongs to neither list. A List for the same reason as
@@ -1972,6 +1972,10 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
}
}
if ("Fail".equals(stateType)) {
validateFailErrorAndCauseFields(statePath, stateDef, errors);
}
if ("Map".equals(stateType)) {
validateMapConcurrency(statePath, stateDef, stateIsJsonata, errors);
if (stateDef.has("ItemReader")) {
@@ -2123,6 +2127,37 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
subWorkflowPath + "/StartAt", errors);
}
/**
* A Fail state resolves its {@code Error} either from the literal {@code Error} field or
* dynamically from {@code ErrorPath}, never both; the same holds for {@code Cause} and
* {@code CausePath}. Real AWS refuses a definition that specifies both at CreateStateMachine.
*/
private static void validateFailErrorAndCauseFields(String statePath, JsonNode stateDef, List<String> errors) {
if (stateDef.has("Error") && stateDef.has("ErrorPath")) {
errors.add("A Fail state cannot include both field 'Error' and 'ErrorPath' at " + statePath);
}
if (stateDef.has("Cause") && stateDef.has("CausePath")) {
errors.add("A Fail state cannot include both field 'Cause' and 'CausePath' at " + statePath);
}
validateFailPathFieldIsString(statePath, stateDef, "ErrorPath", errors);
validateFailPathFieldIsString(statePath, stateDef, "CausePath", errors);
}
/**
* {@code ErrorPath}/{@code CausePath} are reference paths or {@code States.*} intrinsics,
* always given as a JSON string; a non-string value (a number, object, array, or boolean)
* is a definition error AWS rejects at {@code CreateStateMachine}, not something that should
* reach execution and fail there instead.
*/
private static void validateFailPathFieldIsString(String statePath, JsonNode stateDef, String field,
List<String> errors) {
JsonNode value = stateDef.get(field);
if (value != null && !value.isTextual()) {
errors.add(EXPLICIT_LOCATION_MARKER + "Expected value of type [STRING]"
+ MARKER_PAYLOAD_SEPARATOR + statePath + "/" + field);
}
}
private void validateMapConcurrency(String statePath, JsonNode stateDef,
boolean jsonata, List<String> errors) {
if (stateDef.has("MaxConcurrency") && stateDef.has("MaxConcurrencyPath")) {