# Serverless SDK modifies error handling and Cloudwatch logs

**URL:** <https://forum.serverless.com/t/serverless-sdk-modifies-error-handling-and-cloudwatch-logs/11548>\
**Category:** Serverless Framework\
**Tags:** aws\
**Created:** [May 25, 2020, 4:57pm UTC](https://forum.serverless.com/t/serverless-sdk-modifies-error-handling-and-cloudwatch-logs/11548 "2020-05-25T16:57:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattdelaf](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@mattdelaf](https://forum.serverless.com/u/mattdelaf)\
**Post date:** [May 25, 2020, 4:57pm UTC](https://forum.serverless.com/t/serverless-sdk-modifies-error-handling-and-cloudwatch-logs/11548/1 "2020-05-25T16:57:06Z")

</div>

Provider: AWS  
Runtime: NodeJS 12.x

I’m using Typescript, so using the `serverless-plugin-typescript` plugin.

I have Serverless Dashboard setup for monitoring.

Before using Serverless to deploy my functions, when an error was thrown by my handler functions, the error would result in an Invocation error and would appear in CloudWatch logs as :  
ERROR Invoke Error { … my custom error … }

Now, when an error is thrown, the final logs of the failing request in Lambda looks like :  
ERROR Invoke Error   
{  
“errorType”: “TypeError”,  
“errorMessage”: “The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received an instance of InternalServerErrorException”,  
“code”: “ERR\_INVALID\_ARG\_TYPE”,  
“stack”: [  
“TypeError [ERR\_INVALID\_ARG\_TYPE] [ERR\_INVALID\_ARG\_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received an instance of InternalServerErrorException”,  
" at Function.from (buffer.js:327:9)",  
" at e.exports.error (/var/task/serverless\_sdk/index.js:9:140585)",  
" at \_ (/var/task/serverless\_sdk/index.js:9:135767)",  
" at /var/task/serverless\_sdk/index.js:9:137263",  
" at new Promise ()",  
" at /var/task/serverless\_sdk/index.js:9:137248",  
" at processTicksAndRejections (internal/process/task\_queues.js:97:5)"  
]  
}

InternalServerErrorException being the class of the custom error object that I want to throw (actually it’s from the NestJS framework but anyway).

I looks like the serverless sdk is trying to catch this error and do something with it before throwing it again, but some sort of serialization fails and thus the Invocation error is related to this error instead of my actual error.  
It turns out to be pretty difficult to have info about what is happening under the hood with Serverless SDK and what it is trying to do.  
Can someone explain this to me please ? 🙂

---

<div class="post-metadata">

**Author:** ![fuschini](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/fuschini/32/4484_2.png) [@fuschini](https://forum.serverless.com/u/fuschini)\
**Post date:** [August 14, 2020, 7:11pm UTC](https://forum.serverless.com/t/serverless-sdk-modifies-error-handling-and-cloudwatch-logs/11548/2 "2020-08-14T19:11:16Z")

</div>

Check this tutorial out: [https://www.serverless.com/blog/monitor-and-debug-all-serverless-errors](https://www.serverless.com/blog/monitor-and-debug-all-serverless-errors)

Basically you must use `context.captureError(error)` instead of `throw error` in your catch block

---

<div class="post-metadata">

**Author:** ![vicary](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/vicary/32/3543_2.png) [@vicary](https://forum.serverless.com/u/vicary)\
**Post date:** [November 4, 2020, 3:08pm UTC](https://forum.serverless.com/t/serverless-sdk-modifies-error-handling-and-cloudwatch-logs/11548/3 "2020-11-04T15:08:37Z")

</div>

Using `context.captureError(error)` with an async handler results in the warning below.

```auto
WARNING: Callback/response already delivered. Did your function invoke the callback and also return a promise? For more details, see: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html

```

`serverless_sdk` should properly and transparently handles uncaught errors instead of shifting an unnecessary burden on users to catch everything.

The fact that the `Buffer` constructor is really old, and `serverless_sdk` is closed source makes it really uneasy.

---

<div class="post-metadata">

**Author:** ![mattdelaf](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@mattdelaf](https://forum.serverless.com/u/mattdelaf)\
**Post date:** [November 4, 2020, 3:33pm UTC](https://forum.serverless.com/t/serverless-sdk-modifies-error-handling-and-cloudwatch-logs/11548/4 "2020-11-04T15:33:21Z")

</div>

> Basically you must use `context.captureError(error)` instead of `throw error` in your catch block

But I still want to make my lambda fail (because I have lambda - SQS integration and I want the SQS message to be retried for instance), so how do I do this without throwing the error ?
