1

hello I would like to create an unhandledRejection if it returns several errors afterwards script exit I will explain why

process.on ('unhandledRejection', (reason, p) => {
  console.error ('Unhandled Rejection at: Promise', p, 'reason:', reason);
  run();
});

here my script is restarted in case of error but I would like it to stop in case of multiple error afterwards I don't know how to do it if you have an idea

here is an example of in case of error the script stops:

process.on ('unhandledRejection', (reason, p) => {
  console.error ('Unhandled Rejection at: Promise', p, 'reason:', reason);
  process.exit();
});
jfriend00
  • 683,504
  • 96
  • 985
  • 979
x_zone web
  • 25
  • 4

1 Answers1

1

You can use a boolean (flag) this so your "run" only runs once. But if you have two errors right in a row it is unlikely it will finish, so you'd need to do some sort of "wait" on the second, third, etc. if there are other errors.

let flagOneUnhandledReject = false;
let finalProcess = Promise.resolve();
process.on ('unhandledRejection', async (reason, p) => {
  console.error ('Unhandled Rejection at: Promise', p, 'reason:', reason);
  if(!flagOneUnhandledReject){
    flagOneUnhandledReject = true
    finalProcess = run(); // run() needs to return a promise
  }else{
    await finalProcess; // make sure finalProcess doesn't throw any errors
    process.exit();
  }
});
Cody G
  • 8,368
  • 2
  • 35
  • 50
  • hi sorry to bother you so here with this code I found a little problem so I wanted to change it you could help me please – x_zone web May 06 '21 at 22:38
  • https://stackoverflow.com/questions/67426786/i-would-like-to-launch-my-function-but-when-the-code-returns-an-error-503-respon – x_zone web May 06 '21 at 22:39