# How to immediately return with an error?

**URL:** https://forum.serverless.com/t/how-to-immediately-return-with-an-error/1139
**Category:** Serverless Framework
**Tags:** aws
**Created:** [January 18, 2017, 3:37am UTC](https://forum.serverless.com/t/how-to-immediately-return-with-an-error/1139 "2017-01-18T03:37:06Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![hendry](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/hendry/32/343_2.png) [@hendry](https://forum.serverless.com/u/hendry)
#### Post date: [January 18, 2017, 3:37am UTC](https://forum.serverless.com/t/how-to-immediately-return-with-an-error/1139/1 "2017-01-18T03:37:06Z")

</div>

This is a followup on my confusion on how to die() with lambda [{"message": "Internal server error"} on API Gateway responses with 1.5](http://forum.serverless.com/t/message-internal-server-error-on-api-gateway-responses-with-1-5/1117) … this time WITHOUT API Gateway.

Consider the code sample here:

```
$ cat handler.js
'use strict';

var AWS = require('aws-sdk');
AWS.config.region = 'ap-southeast-1';

module.exports.hello = (event, context, callback) => {
        var s3 = new AWS.S3();
        s3.listObjectsV2({ Bucket: "BLAH-BLAH-TRIGGER-AN-ERROR", Prefix: "movies/" }, (err, data) => {
                if (err) {
                        callback(err, null)
                } else {
                        console.log(data)
                }
                callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event });
        });
};
$ serverless invoke local -f hello
{
    "errorMessage": "The specified bucket does not exist",
    "errorType": "Error"
}
{
    "message": "Go Serverless v1.0! Your function executed successfully!",
    "event": ""
}

```

I don’t want ‘Go Serverless v1.0! Your function executed successfully!’ to run. I just want the function to end. How should I be going about it? Many thanks,

---

<div class="post-metadata">

### Author: ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)
#### Post date: [January 18, 2017, 4:16am UTC](https://forum.serverless.com/t/how-to-immediately-return-with-an-error/1139/2 "2017-01-18T04:16:26Z")

</div>

You need to return after your callback.

```javascript
callback(err, null);
return;

```

---

<div class="post-metadata">

### Author: ![hendry](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/hendry/32/343_2.png) [@hendry](https://forum.serverless.com/u/hendry)
#### Post date: [January 18, 2017, 4:42am UTC](https://forum.serverless.com/t/how-to-immediately-return-with-an-error/1139/3 "2017-01-18T04:42:29Z")

</div>

oh… so what does callback actually mean? Why would you use it more than once? 🤔

---

<div class="post-metadata">

### Author: ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)
#### Post date: [January 18, 2017, 5:16am UTC](https://forum.serverless.com/t/how-to-immediately-return-with-an-error/1139/4 "2017-01-18T05:16:55Z")

</div>

Using `callback()` is **optional**. You only need to use it if you want to pass information back to caller. There’s a bunch of information on [http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html#nodejs-prog-model-handler-callback](http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html#nodejs-prog-model-handler-callback) about it.

Importantly, calling `callback()` doesn’t terminate your Lambda function. From what I can tell it just set the response that will be sent to the caller once your function terminates.

---

<div class="post-metadata">

### Author: ![rowanu](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/rowanu/32/75_2.png) [@rowanu](https://forum.serverless.com/u/rowanu)
#### Post date: [January 21, 2017, 11:34pm UTC](https://forum.serverless.com/t/how-to-immediately-return-with-an-error/1139/5 "2017-01-21T23:34:39Z")

</div>

A more common pattern would be the return the callback itself:

```javascript
return callback(err, null);

```

---

<div class="post-metadata">

### Author: ![hendry](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/hendry/32/343_2.png) [@hendry](https://forum.serverless.com/u/hendry)
#### Post date: [January 24, 2017, 7:11am UTC](https://forum.serverless.com/t/how-to-immediately-return-with-an-error/1139/6 "2017-01-24T07:11:08Z")

</div>

That makes sense. Default template should have that, no? Otherwise people will get a nasty surprise like I did.

---

<div class="post-metadata">

### Author: ![freako987](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/freako987/32/2166_2.png) [@freako987](https://forum.serverless.com/u/freako987)
#### Post date: [July 2, 2018, 5:07pm UTC](https://forum.serverless.com/t/how-to-immediately-return-with-an-error/1139/7 "2018-07-02T17:07:48Z")

</div>

I know this is old, but I’m fairly confused here. I have the same logic going on an APIGateway, with this exact pattern (of course the result is not null, but an API response.)

It works when I invoke locally with a mock API request JSON file, but fails with an internal server error when deployed to AWS. I know it is related to this statement because if I get rid of the return, the rest of the function executes. With the return statement, it fails with no errors, just exits (due to the return.) It seems like the callback just isn’t being executed because of the return.

---

<div class="post-metadata">

### Author: ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)
#### Post date: [July 3, 2018, 12:52pm UTC](https://forum.serverless.com/t/how-to-immediately-return-with-an-error/1139/8 "2018-07-03T12:52:01Z")

</div>

@freako987 Are you using Node 8.10? Have you declared your handler `async`? If so you can’t use `callback` and need to return a value. I wrote an article explaining how `async` appears to work with Node 8.10 [How async Lambda handlers work in Node 8.10](https://www.richdevelops.dev/how-async-lambda-handlers-work-in-node-810)

---

<div class="post-metadata">

### Author: ![rowanu](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/rowanu/32/75_2.png) [@rowanu](https://forum.serverless.com/u/rowanu)
#### Post date: [July 3, 2018, 9:34pm UTC](https://forum.serverless.com/t/how-to-immediately-return-with-an-error/1139/9 "2018-07-03T21:34:52Z")

</div>

To add to @buggy’s response, can you share some code? Is your `err` object empty?

---

<div class="post-metadata">

### Author: ![k1m8ee](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/k1m8ee/32/2855_2.png) [@k1m8ee](https://forum.serverless.com/u/k1m8ee)
#### Post date: [December 18, 2018, 1:47am UTC](https://forum.serverless.com/t/how-to-immediately-return-with-an-error/1139/10 "2018-12-18T01:47:13Z")

</div>

Migrated to 8.10 recently, cannot use `return callback(null, response);` nor `callback(null, response); return`.

In the doc, it implies that callback is [supported](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html#nodejs-prog-model-handler-callback), but that is not the case whenever I try to push my lambda with my serverless command.

I created a new callback function on my lambda console and set the environment to `node 8.10`, it worked as expected. I use similar function on serverless project, I see this `Task timed out after 6.01 seconds` on my cloudwatch log.

---

<div class="post-metadata">

### Author: ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)
#### Post date: [December 18, 2018, 9:10am UTC](https://forum.serverless.com/t/how-to-immediately-return-with-an-error/1139/11 "2018-12-18T09:10:45Z")

</div>

@k1m8ee Node 8.10 supports both `async` and `callback` handler syntax. If you want more help you’ll need to post your code.

---

<div class="post-metadata">

### Author: ![k1m8ee](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/k1m8ee/32/2855_2.png) [@k1m8ee](https://forum.serverless.com/u/k1m8ee)
#### Post date: [December 18, 2018, 12:48pm UTC](https://forum.serverless.com/t/how-to-immediately-return-with-an-error/1139/12 "2018-12-18T12:48:23Z")

</div>

Hi buggy, thanks for reply to my post.

I make sure that it runs on nodejs8.10

AWS lambda console;  
exports.handler = (event, context, callback) =\> {  
// TODO implement  
const response = {  
statusCode: 500,  
body: JSON.stringify(‘Hello from Lambda!’),  
};  
// return response;  
callback(null, response);  
};

Local serverless project:

**serverless yml**

```
provider:
   name: aws
   runtime: nodejs8.10

module.exports = {
 hello: hello,
}
const response = {
     statusCode: 500,
     body: JSON.stringify('Hello from Lambda!'),
};
function hello(event, context, callback) => {
    // return response;
    callback(null, response); 
};
// Cloudwatch logs
{
  "errorMessage": "2018-12-18T12:36:56.578Z xxx Task timed out after 6.01 seconds",
  "errorType": "ReferenceError"
}

```

alternatively;  
function hello(event, context, callback) =\> {  
// return response;  
return callback(null, response);  
};

```
function hello(event, context, callback) => {
    // return response;
    callback(null, response); 
    return ;
};

```

But if i try;  
async function hello(event, context, callback) =\> {  
try {  
throw ‘Ops’;  
} catch (e) {  
return response;  
}  
};

vola it works.

---

<div class="post-metadata">

### Author: ![jonalberghini](https://avatars.discourse-cdn.com/v4/letter/j/6a8cbe/32.png) [@jonalberghini](https://forum.serverless.com/u/jonalberghini)
#### Post date: [April 9, 2019, 11:54pm UTC](https://forum.serverless.com/t/how-to-immediately-return-with-an-error/1139/13 "2019-04-09T23:54:40Z")

</div>

Does anyone know how to write an authorizer that is defined async?
