|
| 1 | +import * as cdk from '@aws-cdk/core'; |
| 2 | +import * as tasks from '@aws-cdk/aws-stepfunctions-tasks'; |
| 3 | +import { RetryProps } from '@aws-cdk/aws-stepfunctions' |
| 4 | +import { ResilientLambdaTask } from '../construct/ResilientLambdaTask' |
| 5 | + |
| 6 | +export interface ResilienceLambdaCheckerProps { |
| 7 | + readonly fail?: boolean; |
| 8 | +} |
| 9 | + |
| 10 | +export class ResilienceLambdaChecker implements cdk.IAspect { |
| 11 | + |
| 12 | + private readonly _fail?: boolean; |
| 13 | + |
| 14 | + constructor(props?: ResilienceLambdaCheckerProps) { |
| 15 | + this._fail = props?.fail |
| 16 | + } |
| 17 | + |
| 18 | + public visit(construct: cdk.IConstruct): void { |
| 19 | + |
| 20 | + if (construct instanceof tasks.LambdaInvoke) { |
| 21 | + const reporter = this._fail ? construct.node.addError : construct.node.addWarning; |
| 22 | + |
| 23 | + const retries = this.getRetryConfiguration(construct); |
| 24 | + if (retries.length > 0) { |
| 25 | + const unhandledErrors = this.getUnhandledTransientErrors(retries); |
| 26 | + |
| 27 | + if (unhandledErrors.length > 0) { |
| 28 | + reporter.apply(construct.node, [`Missing retry for transient errors: ${unhandledErrors}.`]); |
| 29 | + } |
| 30 | + } else { |
| 31 | + reporter.apply(construct.node, ['No retry for AWS Lambda transient errors defined - consider using ResilientLambdaTask construct.']); |
| 32 | + //ResilientLambdaTask.addDefaultRetry(construct); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private getUnhandledTransientErrors(retries: Array<RetryProps>): Array<string> { |
| 38 | + return ResilientLambdaTask.TransientErrors.filter(transientError => |
| 39 | + retries.every(config => !config.errors?.includes(transientError))); |
| 40 | + } |
| 41 | + |
| 42 | + private getRetryConfiguration(construct: cdk.IConstruct): Array<RetryProps> { |
| 43 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 44 | + return (construct as any).retries as Array<RetryProps> || [] |
| 45 | + } |
| 46 | +} |
0 commit comments