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

153
Vistas
proper way of handling fatal errors in Node.js

According to Node.js documentation usually, it's not a good idea to use process.exit() because async IO operations like console.log() or other logging methods (Pino logging library in my case) might get skipped, the process exits before they complete their task. I was wondering what is the best way to handle errors inside a function. My final goal is to handle errors in a function and if the error is a fatal error then exit the process.

I wrote a simplified version of what I currently think is the best option (similar to the solution explained in Node.js docs):

const validateUserInput = (input) => {
  try {
    if (input === 'wrong') { throw new Error('sample error'); } // simplified
  } catch (err) {
    console.log('last message');
    // process.exit(1) --> using this might skip IO operation like the log on previous line
    process.exitCode = 1;
    return false;
  }
  return true;
};

if (validateUserInput('wrong')) {
  // rest of the code
}

// nothing should be written here
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

SIGTERM is the signal that tells a process to gracefully terminate. It is the signal that's sent from process managers like upstart or supervisord and many others.

You can send this signal from inside the program, in another function: read more

process.kill(process.pid, 'SIGTERM')

You can also setup your own listeners for this event, and perform other tasks.

process.on('SIGTERM', () => {
  server.close(() => {
    console.log('Shutting down the application ...')
  })
})

But SIGTERM is not supported on windows, so you can use SIGINT instead, here is a useful info about SIGINT

After the SIGINT signal is received, the Node.js server initiates a shutdown sequence and enters shutdown mode. All of the existing requests are completed and no new requests are entertained. When the last request completes, the server closes all of the connections and shuts down. read more

Mor about processes here

about 4 years ago · Juan Pablo Isaza Denunciar

0

If a fatal error occurs, the application will need to be terminated gracefully to prevent default behavior from happening and allow additional statements to execute first. You can achieve this by creating a simple event listener:

// For app termination
const gracefulShutdown = (msg, callback) => {
    server.close(() => {
        //execute additional code here
        console.log(`Application disconnected through ${msg}`);
        callback();
    })
}

process.on('SIGINT', () => {
    //execute additional code here
    gracefulShutdown('app termination', () => {
        process.exitCode = 1;
    })
})

about 4 years ago · Juan Pablo Isaza 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