Sunday, 2 April 2017

How setup Node.js error handling?

I have node 7.8.0 and I have read Node.js Best Practice Exception Handling

Still I can't seem to understand error handling in node.js.

For my 'http' I have server.on('error', () => console.log('Here I should be replacing the Error console.'));

For 404 I have this code

app.use((req, res, next) => {
  let err = new Error('Not Found');
  err.status = 404;
  next(err);
});
app.use((err, req, res) => {
  res.status(err.status);
  res.render('error');
});

404 is being caught, it creates the Error object and then it's handled by rendering the error view correctly as I want. But it also throws error log to my console and I have no idea where that is coming from and how to stop it.

I would like to use my own error handling and never actually write errors like 404 into the console.

Codes that I tried and don't seem to work.

var server = http.createServer(app);
const onError = () => console.log('Here I should be replacing the Error console.');
server.on('error', onError);
server.on('uncaughtException', onError);
process.on('uncaughtException', onError);



via Ragnar

No comments:

Post a Comment