Error handling postresql and nodejs query
09:19 30 Oct 2018

I seem to be struggling to handle any database errors within my app. I have a simple query that updates a users password

// Update Users Password
async function updatePassword(password, id) {
  const client = await pool.connect();
  const sql = format('UPDATE users SET password = $1 WHERE id = $2');
  try {
    await pool.query(sql, [password, id]);
  } catch (e) {
    console.error('Error Occurred', e);
    client.release();
  }
}

Server side I have this in my routes.js

const newPassword = bcrypt.hashSync(req.body.update_password, 10);
try {
  await queries.updatePassword(newPassword, req.body.user_id);
  res.status(200).send({ result: 'Password Updated Successfully' });
} catch (e) {
  res.status(400).send({ result: 'Oops, please try again' });
}

If there are no database errors then this works as expected, but if for example i supply a string as my id (forcing the error) then I get this error printed to the console as expected

Error Occurred { error: invalid input syntax for integer: "string"

but still res.status(200).send({ result: 'Password Updated Successfully' }); is sent.

Logged as

POST /update_password 200

How can I capture the error as I thought I was being that it's in the try/catch block ?

node.js postgresql node-postgres