简体   繁体   中英

Throwing errors using async/await in AWS Lambda node.js function

I am trying to throw an error to the calling function and the error is not getting captured. The exception is not propagating to the calling function.

'use strict';

const { Pool } = require('pg');
const pool = new Pool();

var result;

exports.handler = async (event) => {
   
  var payload = event;
  try{
     result = await insertOrder(payload, 'test');
   }
   catch (err) {
     console.error("Error from main: " + err);
     throw err ;
  }
 return result;
};


async function insertOrder(payload, name)
{ 
 const client = await pool.connect();
 try{
    const queryString = {
        text: "INSERT INTO public.orders(payload, shop_name)" +
              "VALUES ($1, $2) RETURNING id",
        values: [payload, name],
    };

    const result = await client.query(queryString);       
    var orderId = result.rows[0].id;
    
  }
  catch (err) {
    await client.query('ROLLBACK');
    console.log("Error from child: " + err);
    throw err;
  }
  finally {
    client.release();
    return orderId;
  }
}

Here is what is written to the log:

INFO Error from child: error: INSERT has more target columns than expressions

The console.error in the calling function is not written to the log. What am I am missing? TIA!

Moving return orderId; to try block solved my issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM