aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.65k stars 3.91k forks source link

aws-stepfunctions-tasks: addCatch does not support `resultPath: DISCARD` #26760

Closed kopertop closed 1 year ago

kopertop commented 1 year ago

Describe the bug

The LambdaInvoke package allows for an addCatch which is documented as allowing a CatchProps to include a resultPath special value of DISCARD

    /**
     * JSONPath expression to indicate where to inject the error data
     *
     * May also be the special value DISCARD, which will cause the error
     * data to be discarded.
     *
     * @default $
     */
    readonly resultPath?: string;

HOWEVER, when trying to add a catch with this special value of DISCARD for the resultPath it tosses an error:

Error: Expected JSON path to start with '$', got: DISCARD

Trace: Error: Expected JSON path to start with '$', got: DISCARD
    at renderJsonPath (node_modules/.pnpm/aws-cdk-lib@2.91.0_constructs@10.2.69/node_modules/aws-cdk-lib/aws-stepfunctions/lib/states/state.js:1:8225)
    at renderCatch (node_modules/.pnpm/aws-cdk-lib@2.91.0_constructs@10.2.69/node_modules/aws-cdk-lib/aws-stepfunctions/lib/states/state.js:1:7612)

Expected Behavior

The step function definition should use the special null value as the resultPath when passed the special DISCARD value.

Current Behavior

Attempted deploy causes the CDK to fail during validation:

Error: Expected JSON path to start with '$', got: DISCARD

Trace: Error: Expected JSON path to start with '$', got: DISCARD
    at renderJsonPath (node_modules/.pnpm/aws-cdk-lib@2.91.0_constructs@10.2.69/node_modules/aws-cdk-lib/aws-stepfunctions/lib/states/state.js:1:8225)
    at renderCatch (node_modules/.pnpm/aws-cdk-lib@2.91.0_constructs@10.2.69/node_modules/aws-cdk-lib/aws-stepfunctions/lib/states/state.js:1:7612)

Reproduction Steps

import { Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';

import { LambdaInvoke } from 'aws-cdk-lib/aws-stepfunctions-tasks';
import { DISCARD, StateMachine } from 'aws-cdk-lib/aws-stepfunctions';
import { Code, Function, Runtime } from 'aws-cdk-lib/aws-lambda';

export class SampleAppStack extends Stack {
    constructor(scope: Construct, id: string, props?: StackProps) {
        super(scope, id, props);
        const taskA = new LambdaInvoke(this, 'taskA', {
            resultPath: '$.Payload',
            lambdaFunction: new Function(this, 'taskAFnc', {
                runtime: Runtime.NODEJS_18_X,
                code: Code.fromInline('exports.handler = async () => { throw new Error("Error"); };'),
                handler: 'index.handler',
            }),
        });
        const taskB = new LambdaInvoke(this, 'taskB', {
            resultPath: '$.Payload',
            lambdaFunction: new Function(this, 'taskBFnc', {
                runtime: Runtime.NODEJS_18_X,
                code: Code.fromInline('exports.handler = async () => "OK";'),
                handler: 'index.handler',
            }),
        });
        taskA.addCatch(taskB, {
            resultPath: DISCARD,
        });

        const stateMachine = new StateMachine(this, 'SimpleStateMachine', {
            definition: taskA,
        });
    }
}

then run cdk synth

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.91.0 (build eea12ea)

Framework Version

No response

Node.js Version

v18.17.0

OS

Darwin razorcrest.local 22.5.0 Darwin Kernel Version 22.5.0: Thu Jun 8 22:22:23 PDT 2023; root:xnu-8796.121.3~7/RELEASE_ARM64_T6020 arm64

Language

Typescript

Language Version

TypeScript (5.1.6)

Other information

No response

kopertop commented 1 year ago

It looks like the correct code to use is JsonPath.DISCARD rather than just DISCARD. Solution may be to remove the extraneous DISCARD value that's also included, and update the documentation in the addCatch function to indicate using JsonPath.DISCARD

peterwoodworth commented 1 year ago

Thanks for reporting the issue

I couldn't find anywhere that we use this variable https://github.com/aws/aws-cdk/blob/eea12ea799973dff27aa4dedf03221e277ebaa0c/packages/aws-cdk-lib/aws-stepfunctions/lib/types.ts#L160

I'm not sure if this constant has any legitimate use cases - I can't find anywhere it's being used within our codebase, and we only document JsonPath.DISCARD in the readmes and snippets. If it doesn't have any legitimate use cases, we should deprecate it, and perhaps make it more clear in the CatchProps snippet that we want JsonPath.DISCARD

github-actions[bot] commented 1 year ago

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.