title: How to send custom error from serverless function in node.js?
body: <p>I am trying to throw an error from my lambda function to catch as a response with a status code of the error.</p>
  <p>But every time a make an API call from the postman I am getting <code>message: Internal server error</code> and status code <code>502</code>. If a try to send a custom error from my <code>catchHandler</code>.</p>
  <pre><code> const errorHandler = (
    errorCode = 500,
    errorApi = null,
    errorMessage = &quot;server error&quot;
  ) =&gt; {
    let parsedError = null;
    // body:JSON.stringify({ parsedError}) || JSON.stringify({ errorMessage}) || &quot;server error&quot;,
    console.log({
      statusCode:errorCode,
      body: parsedError || errorMessage || &quot;server error&quot;,
    })
    return {
      statusCode:errorCode,
      body: parsedError || errorMessage || &quot;server error&quot;,
    };
  };
  module.exports.handler = async (event) =&gt; {
      try{
      await initialiseMongodbDatabase()
      const req = await lambdaRequestProcessor(event);
      req.OriginalUrl=&quot;/resourceLib/blog/&quot;;
      const res = await fetchSingleBlog(req,null);
     return responseHandler(res);
    } catch (error) {
      return errorHandler(error.errorCode, error.errorApi, error);
    }
  </code></pre>
  <p>event:</p>
  <pre><code>{
    &quot;queryStringParameters&quot;: {
  
    }
  }
  </code></pre>
  <p>Response(lambda test):</p>
  <pre><code>{
    &quot;statusCode&quot;: 422,
    &quot;body&quot;: {
      &quot;errorCode&quot;: 422,
      &quot;errorAPI&quot;: &quot;/resourceLib/blog/&quot;,
      &quot;error&quot;: &quot;ValidationError: \&quot;link\&quot; is required&quot;
    }
  }
  </code></pre>
  <p>Response(POSTMAN):</p>
  <pre><code>{
      &quot;message&quot;: &quot;Internal server error&quot;
  }
  </code></pre>
  <p>How can I send the same response to POSTMAN?</p>