Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

384
Vistas
How do I catch psql errors in node?

I'm currently writing a backend node server with a Postgresql database where I've attempted to make a registration API set-up. I want to be able to catch errors that are caused by unique constraint violations. How can I do that?

function createMember(body, callBack){  
  // This function adds someone who is newly registered to the database.

  var id;
  var sql = 'INSERT INTO member (fname, lname, phone, email, age, gender) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id;';
  db.query(sql, [body.fname, body.lname, body.phone, body.email, body.age, body.gender]).then(res => {
      id = res.rows[0].id;
      if (id) {
        callBack(body);
        console.log("New member with id: " + id);
      }
  }).catch(e => {
      if (the error is a unique constraint violation){
        console.log("\n ERROR! \n Individual with name: " + body.fname + " " + body.lname + " and phone #: " + body.phone + " is a duplicate member. \n");
        callBack("Duplicate");
        return;
      }
      console.log("\n \n ERROR! \n Individual with name: " + body.fname + " " + body.lname + " and phone #: " + body.phone + " could not be added. \n", e);
      callBack(false);
      return e;
  })
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

So since I haven't found any answers to this, I think I can share mine.

Each error in Postgresql has an error code which can be found at: https://www.postgresql.org/docs/12/errcodes-appendix.html

If you look at the error you get when there is a unique key violation you may notice that the code is "23505".

Simply add a check in your catch block to see if the error has a code of "23505".

function createMember(body, callBack){  
  // This function adds someone who is newly registered to the database.

  var id;
  var sql = 'INSERT INTO member (fname, lname, phone, email, age, gender) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id;';
  db.query(sql, [body.fname, body.lname, body.phone, body.email, body.age, body.gender]).then(res => {
      id = res.rows[0].id;
      if (id) {
        callBack(body);
        console.log("New member with id: " + id);
      }
  }).catch(e => {
      if (e.code == '23505'){
        console.log("\n ERROR! \n Individual with name: " + body.fname + " " + body.lname + " and phone #: " + body.phone + " is a duplicate member. \n");
        callBack("Duplicate");
        return;
      }
      console.log("\n \n ERROR! \n Individual with name: " + body.fname + " " + body.lname + " and phone #: " + body.phone + " cannot be added. \n", e);
      callBack(false);
      return e;
  })
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda