title: How do I return errors on an AWS Lambda function written in Node.js through the AWS API Gateway using the Serverless framework?
body: <p>I'm writing an API for internal use, and for the first time I'm using the <a href="https://serverless.com" rel="noreferrer">serverless framework</a>. I'm writing a Lambda function in Node.js, and using the AWS API Gateway to connect to it. </p>
  
  <p>In some cases I want to return a custom error message, and I'm trying to write a function that would allow me to do that. Right now, any time the Lambda process fails, I get a standard message from the API. In the code, if I try to kill the process using <code>process.exit(1)</code>, I get a generic error, even if I've already returned an error using <code>callback()</code>:</p>
  
  <pre><code>{
      "message": "Internal server error"
  }
  </code></pre>
  
  <p>If I don't use <code>process.exit(1)</code>, I see the error I returned via <code>callback()</code> in the logs, but the process continues, ultimately timing out: </p>
  
  <pre><code>{
      "message": "Endpoint request timed out"
  }
  </code></pre>
  
  <p>I've tried a few different ways of returning an error using the <code>callback()</code> method, but so far I haven't been successful. I've tried this method:</p>
  
  <pre><code>async function return_error(callback, context, error, returnCode){
    console.error("FATAL ERROR: ", error);
    let ErrorObj = {
      errorType : "InternalServerError",
      httpStatus : 500,
      requestId : context.awsRequestId,
      errorMessage : error
  }
    callback(JSON.stringify(ErrorObj));
    process.exit(1);
  }
  </code></pre>
  
  <p>and this one: </p>
  
  <pre><code>async function return_error(callback, error, returnCode){
    console.error("FATAL ERROR: ", error);
    callback({
      isBase64Encoded: false,
      statusCode: returnCode,
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({"Error Message:": error})
    }, null);
    process.exit(1);
  }
  </code></pre>
  
  <p>(Sorry about the minor syntax changes between the two.)</p>
  
  <p>So far, I haven't been able to return any error to the user via the API. My error always get's logged, and the function continues. Any help would be appreciated. Thank you!</p>
  
  <p>For reference, the relevant parts of my serverless.yml file:</p>
  
  <pre><code>service: #Name of service
  
  
  provider:
    name: aws
    runtime: nodejs8.10
    role: #ARN of Iam role
  
  functions:
    screenshot:
      handler: #Name of handler
      timeout: 30
      memorySize: 1280
      reservedConcurrency: 10
      events:
        - http: 
            method: get
            path: #path
            contentHandling: CONVERT_TO_BINARY
            authorizer:
              type: aws_iam
  
  plugins:
    - serverless-plugin-chrome
    - serverless-apigw-binary
    - serverless-apigwy-binary
  package:
    exclude:
      - node_modules/puppeteer/.local-chromium/** 
  
  custom:
    apigwBinary:
      types:
        - '*/*'
  </code></pre>