dherault / serverless-offline

Emulate AWS λ and API Gateway locally when developing your Serverless project
MIT License
5.16k stars 794 forks source link

errorType overriden on custom typescript exception #1631

Open C4n4rd0 opened 1 year ago

C4n4rd0 commented 1 year ago

Current Behavior

In a step function, with retriers defined, orchestrating typescript lambdas, if the lambda throws a custom exception, the retrier does not work because serverless-offline changes the errorType to "Error" instead of populating the value of err.name. The result is that the lambda is executed only once and it fails.

Here is the log output: [Serverless Step Functions Local] 2022-12-16 17:41:19.675: arn:aws:states:...:my-lambda:... : {"Type":"TaskFailed","PreviousEventId":18,"TaskFailedEventDetails":{"ResourceType":"lambda","Resource":"invoke","Error":"Error","Cause":"{\"errorType\":\"Error\",\"errorMessage\":\"INFO: Lambda is initializing your function. It will be ready to invoke shortly.\",

Sample Code

plugins:
  - serverless-plugin-typescript
  - serverless-plugin-tracing
  - serverless-plugin-conditional-functions
  - serverless-s3-local
  - serverless-step-functions
  - serverless-step-functions-local
  - serverless-offline-elasticmq
  - serverless-offline-sqs
  - serverless-offline
  - serverless-iam-roles-per-function
  ...
                  Type: Task,
                  Resource: arn:aws:states:::lambda:invoke,
                  TimeoutSeconds: 60,
                  HeartbeatSeconds: 10,
                  Retry: [{
                    ErrorEquals: [
                      "Lambda.ServiceException",
                      "Lambda.AWSLambdaException",
                      "Lambda.SdkClientException",
                      "Lambda.CodeArtifactUserPendingException",
                      "CodeArtifactUserPendingException"
                    ],
                    IntervalSeconds: 3,
                    MaxAttempts: 3,
                    BackoffRate: 2

abstract class MockAWSException extends Error implements AWSError { readonly code: string; readonly retryable?: boolean | undefined; readonly statusCode?: number | undefined; readonly time: Date; readonly requestId?: string | undefined; protected constructor( readonly message: string, { retryable, statusCode, }: { retryable?: boolean | undefined; statusCode?: number | undefined; }, ) { super(message); this.code = this.constructor.name; this.name = this.constructor.name; this.time = new Date(); this.requestId = uuid(); this.statusCode = statusCode; this.retryable = retryable; Error.captureStackTrace(this, this.constructor); } }

export class CodeArtifactUserPendingException extends MockAWSException { constructor(readonly message: string) { super(message, { statusCode: 500, retryable: true, }); } }



**Expected behavior/code**

The retriers defined in the step functions definition should be applied, meaning that the lambda should be executed 3 times before failing. And the log output should be:

`[Serverless Step Functions Local] 2022-12-16 17:41:19.675: arn:aws:states:...:my-lambda:... : {"Type":"TaskFailed","PreviousEventId":18,"TaskFailedEventDetails":{"ResourceType":"lambda","Resource":"invoke","Error":"CodeArtifactUserPendingException","Cause":"{\"errorType\":\"CodeArtifactUserPendingException\",\"errorMessage\":\"INFO: Lambda is initializing your function. It will be ready to invoke shortly.\",`

**Environment**

- `serverless` version: 3.21.0
- `serverless-offline` version: 8.8.1
- `node.js` version: 14.18.1

**Possible Solution**

Line 60, in the following file:
src/lambda/routes/invocations/InvocationsController.js
replace `errorType: 'Error',` with `errorType: err.name !== undefined ? err.name : 'Error',`
dnalborczyk commented 1 year ago

thank you for creating this issue @C4n4rd0 unfortunately v8.8.1 is not maintained anymore. do you know if your issue applies to the latest version which currently is v12.0.3 as well? if that's the case, could you create a small repro repository which outlines your issue? labeling as bug in the meanwhile.

C4n4rd0 commented 1 year ago

Yes the issue applies to the lastedt version. The proposed solution is based on the latest code base. I'll add a repo allowing to reproduce the issue in few days.

C4n4rd0 commented 1 year ago

As requested, here is a repro repository with the last serverless offline version: https://github.com/C4n4rd0/bug-sls-offline-1631

madkins-webscale commented 11 months ago

I'm experiencing this issue too - is there any workaround?