Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(stepfunctions): cannot use intrinsic functions in Fail state #30210

Merged
merged 7 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/aws-cdk-lib/aws-stepfunctions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,16 @@ const fail = new sfn.Fail(this, 'Fail', {
});
```

You can also use an intrinsic function that returns a string to specify CausePath and ErrorPath.
The available functions include States.Format, States.JsonToString, States.ArrayGetItem, States.Base64Encode, States.Base64Decode, States.Hash, and States.UUID.

```ts
const fail = new sfn.Fail(this, 'Fail', {
errorPath: sfn.JsonPath.format('error: {}.', sfn.JsonPath.stringAt('$.someError')),
causePath: "States.Format('cause: {}.', $.someCause)",
});
```

### Map

A `Map` state can be used to run a set of steps for each element of an input array.
Expand Down
34 changes: 31 additions & 3 deletions packages/aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Construct } from 'constructs';
import { StateType } from './private/state-type';
import { renderJsonPath, State } from './state';
import { Token } from '../../../core';
import { INextable } from '../types';

/**
Expand Down Expand Up @@ -31,6 +32,9 @@ export interface FailProps {
/**
* JsonPath expression to select part of the state to be the error to this state.
*
* You can also use an intrinsic function that returns a string to specify this property.
* The allowed functions include States.Format, States.JsonToString, States.ArrayGetItem, States.Base64Encode, States.Base64Decode, States.Hash, and States.UUID.
*
* @default - No error path
*/
readonly errorPath?: string;
Expand All @@ -45,6 +49,9 @@ export interface FailProps {
/**
* JsonPath expression to select part of the state to be the cause to this state.
*
* You can also use an intrinsic function that returns a string to specify this property.
* The allowed functions include States.Format, States.JsonToString, States.ArrayGetItem, States.Base64Encode, States.Base64Decode, States.Hash, and States.UUID.
*
* @default - No cause path
*/
readonly causePath?: string;
Expand Down Expand Up @@ -80,9 +87,30 @@ export class Fail extends State {
Type: StateType.FAIL,
Comment: this.comment,
Error: this.error,
ErrorPath: renderJsonPath(this.errorPath),
ErrorPath: this.isIntrinsicString(this.errorPath) ? this.errorPath : renderJsonPath(this.errorPath),
Cause: this.cause,
CausePath: renderJsonPath(this.causePath),
CausePath: this.isIntrinsicString(this.causePath) ? this.causePath : renderJsonPath(this.causePath),
};
}
}

/**
* Validate this state
*/
protected validateState(): string[] {
const errors = super.validateState();

if (this.error && this.errorPath) {
errors.push('Fail state cannot have both error and errorPath');
}

if (this.cause && this.causePath) {
errors.push('Fail state cannot have both cause and causePath');
}

return errors;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check for the supported intrinsic functions here? Currently passing in any string that doesn't start with '$' would still pass, correct? I'd like to see a test case where the value is a plain string (neither an intrinsic function nor Json path) to understand the expected behaviour.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, correct.
I added the validation along with the test because validation is possible for strings.
However, the validation checks only the start of the string.
This is because a parser is needed if we also want to check the syntax of intrinsics.

private isIntrinsicString(jsonPath?: string): boolean {
return !Token.isUnresolved(jsonPath) && !jsonPath?.startsWith('$');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,62 @@ describe('State Machine Resources', () => {
});
}),

test.each([
[
"States.Format('error: {}.', $.error)",
"States.Format('cause: {}.', $.cause)",
],
[
stepfunctions.JsonPath.format('error: {}.', stepfunctions.JsonPath.stringAt('$.error')),
stepfunctions.JsonPath.format('cause: {}.', stepfunctions.JsonPath.stringAt('$.cause')),
],
])('Fail should render ErrorPath / CausePath correctly when specifying ErrorPath / CausePath using intrinsics', (errorPath, causePath) => {
// GIVEN
const stack = new cdk.Stack();
const fail = new stepfunctions.Fail(stack, 'Fail', {
errorPath,
causePath,
});

// WHEN
const failState = stack.resolve(fail.toStateJson());

// THEN
expect(failState).toStrictEqual({
CausePath: "States.Format('cause: {}.', $.cause)",
ErrorPath: "States.Format('error: {}.', $.error)",
Type: 'Fail',
});
}),

test('fails in synthesis if error and errorPath are defined in Fail state', () => {
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app);

// WHEN
new stepfunctions.Fail(stack, 'Fail', {
error: 'error',
errorPath: '$.error',
});

expect(() => app.synth()).toThrow(/Fail state cannot have both error and errorPath/);
}),

test('fails in synthesis if cause and causePath are defined in Fail state', () => {
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app);

// WHEN
new stepfunctions.Fail(stack, 'Fail', {
cause: 'cause',
causePath: '$.cause',
});

expect(() => app.synth()).toThrow(/Fail state cannot have both cause and causePath/);
}),

testDeprecated('Task should render InputPath / Parameters / OutputPath correctly', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down Expand Up @@ -721,7 +777,6 @@ describe('State Machine Resources', () => {
],
});
});

});

interface FakeTaskProps extends stepfunctions.TaskStateBaseProps {
Expand Down
Loading