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 #30193

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
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
@@ -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: {}.', stepfunctions.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.
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';

/**
@@ -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;
@@ -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;
@@ -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;
}

private isIntrinsicString(jsonPath?: string): boolean {
return !Token.isUnresolved(jsonPath) && !jsonPath?.startsWith('$');
}
}
Original file line number Diff line number Diff line change
@@ -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 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();
@@ -721,7 +777,6 @@ describe('State Machine Resources', () => {
],
});
});

});

interface FakeTaskProps extends stepfunctions.TaskStateBaseProps {