title: Unable to reinitialize and return error Response from my catch statement
body: <p>In my lambda node code, I am trying to make a call to my SQL DB and retrieve my data.</p>
  <p>I am using promises and am able to get the data successfully. However, I am not able to implement error handling as per the requirements.</p>
  <p>My requirements are that -</p>
  <ol>
  <li>At 'function' level, I return response through 1 variable.</li>
  <li>Have one catch statement to catch any errors in the Promise Chain</li>
  <li>In case of error, include it and send back as a JSON object.</li>
  </ol>
  <p>Below is my code:</p>
  <pre><code>'use script';
  const mysql = require('mysql');
  let res, SQL;
  
  module.exports = async (event, context, callback) =&gt; {
      const connection = mysql.createConnection ({
          host: process.env.HOST,
          user: process.env.DBUSER,
          password: process.env.DBPASSWORD,
          database: process.env.INSTANCE,
          port: process.env.PORT,
          connectionLimit: process.env.CONNECTION_LIMIT,   
          multipleStatements: false
      });
  
      SQL = ` SELECT 
                  *
              FROM
                  supplie
              WHERE
                  isActive=1;`;
                      
      await new Promise( (resolve, reject) =&gt; {
      
          connection.connect(function (err) {   
                  if (err) { console.log(err); }        
          
              connection.query(SQL, (err, result) =&gt; {
                  console.log(err);
                  resolve(result);
              });
          });
      }).then( (result, err ) =&gt; {
          {
              res = {
                  &quot;statusCode&quot; : 200,
                  &quot;body&quot;: JSON.stringify(result),
                  &quot;headers&quot;  : {
                      &quot;Access-Control-Allow-Origin&quot; : &quot;*&quot;
                  }
              };
              return res;   
          }
      }).catch ( err =&gt; {
          console.error('Something Went Wrong');
          console.log(err);
          res = {
              &quot;statusCode&quot; : 404,
              &quot;body&quot;: JSON.stringify('Something went wrong!'),
              &quot;headers&quot;  : {
                  &quot;Access-Control-Allow-Origin&quot; : &quot;*&quot;
              }
          };
          throw err;
      }).finally( () =&gt; {
          return new Promise((resolve, reject) =&gt; {
              connection.end(error =&gt; error ? reject(error) : resolve());
          });
      });
      // returning response to the function
      return res;
  };
  </code></pre>
  <p>Below is the logs and output:
  Response:</p>
  <pre><code>{
    &quot;statusCode&quot;: 200,
    &quot;headers&quot;: {
      &quot;Access-Control-Allow-Origin&quot;: &quot;*&quot;
    }
  }
  </code></pre>
  <p>Request ID:</p>
  <pre><code>&quot;1fc0c996-8e75-4c0d-b9c9-3757db0a4809&quot;
  
  Function logs:
  START RequestId: 1fc0c996-8e75-4c0d-b9c9-3757db0a4809 Version: $LATEST
  2020-07-22T17:46:21.117Z    1fc0c996-8e75-4c0d-b9c9-3757db0a4809    INFO    Error: ER_NO_SUCH_TABLE: Table 'supplier_svc_db_dev.supplie' doesn't exist
      at Query.Sequence._packetToError (/var/task/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
      at Query.ErrorPacket (/var/task/node_modules/mysql/lib/protocol/sequences/Query.js:79:18)
      at Protocol._parsePacket (/var/task/node_modules/mysql/lib/protocol/Protocol.js:291:23)
      at Parser._parsePacket (/var/task/node_modules/mysql/lib/protocol/Parser.js:433:10)
      at Parser.write (/var/task/node_modules/mysql/lib/protocol/Parser.js:43:10)
      at Protocol.write (/var/task/node_modules/mysql/lib/protocol/Protocol.js:38:16)
      at Socket.&lt;anonymous&gt; (/var/task/node_modules/mysql/lib/Connection.js:88:28)
      at Socket.&lt;anonymous&gt; (/var/task/node_modules/mysql/lib/Connection.js:526:10)
      at Socket.emit (events.js:310:20)
      at addChunk (_stream_readable.js:286:12)
      --------------------
      at Protocol._enqueue (/var/task/node_modules/mysql/lib/protocol/Protocol.js:144:48)
      at Connection.query (/var/task/node_modules/mysql/lib/Connection.js:198:25)
      at Handshake.&lt;anonymous&gt; (/var/task/helpers/testPromise.js:28:24)
      at Handshake.&lt;anonymous&gt; (/var/task/node_modules/mysql/lib/Connection.js:526:10)
      at Handshake._callback (/var/task/node_modules/mysql/lib/Connection.js:488:16)
      at Handshake.Sequence.end (/var/task/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)
      at Handshake.Sequence.OkPacket (/var/task/node_modules/mysql/lib/protocol/sequences/Sequence.js:92:8)
      at Protocol._parsePacket (/var/task/node_modules/mysql/lib/protocol/Protocol.js:291:23)
      at Parser._parsePacket (/var/task/node_modules/mysql/lib/protocol/Parser.js:433:10)
      at Parser.write (/var/task/node_modules/mysql/lib/protocol/Parser.js:43:10) {
    code: 'ER_NO_SUCH_TABLE',
    errno: 1146,
    sqlMessage: &quot;Table 'supplier_svc_db_dev.supplie' doesn't exist&quot;,
    sqlState: '42S02',
    index: 0,
    sql: ' SELECT \n' +
      '                *\n' +
      '            FROM\n' +
      '                supplie\n' +
      '            WHERE\n' +
      '                isActive=1;'
  }
  END RequestId: 1fc0c996-8e75-4c0d-b9c9-3757db0a4809
  REPORT RequestId: 1fc0c996-8e75-4c0d-b9c9-3757db0a4809  Duration: 805.03 ms Billed Duration: 900 ms Memory Size: 1024 MB    Max Memory Used: 74 MB  Init Duration: 142.20 ms    
  </code></pre>
  <p>Expected Output:</p>
  <pre><code>    {
          &quot;statusCode&quot; : 404,
          &quot;body&quot;: JSON.stringify('Something went wrong!'),
          &quot;headers&quot;  : {
              &quot;Access-Control-Allow-Origin&quot; : &quot;*&quot;
          }
      };
  </code></pre>
  <p>Any help would be appreciated.</p>
  <p>Warm Regards,
  Adi</p>