Error handling for Node.js applications using Raygun

  • 2020-06-19 09:47:00
  • OfStack

With our raygun4node package, there is a convenient way to send your ES3en.js error to Raygun. It can be easily installed using npm:


npm install raygun

It gives you an raygun client that you can use to configure your API key and can be used to send error messages manually, but later you might say, "I don't want to send all my errors manually to Raygun, that sounds like a lot of work!" If you're using ES16en. js, you can easily address this concern with express's processor.


var raygun = require('raygun');
var raygunClient = new raygun.Client().init({ apiKey: 'your API key' });
app.use(raygunClient.expressHandler);

In other cases, you might just want to listen for an exception uncaughtException that is not caught and send an error message.


var raygun = require('raygun');
var raygunClient = new raygun.Client().init({ apiKey: 'your API key' });
process.on('uncaughtException', function(err) {
 raygunClient.send(err);
});
 

If you're going to start to do this, then you must understand its meaning. But one time bubble 1 straight back to the event loop, the event will be sent. If you add a listener for this event, then the default action will not happen again. The default action when print out the call stack information and exit process. If continue to trigger this after, then your node process will be in a state of 1 has not been defined. node. js document mentioned that you should not use this stuff, The suggested alternative is to use the domain domains. The following is a small but simple example where you can see how the raygun client ADAPTS to your use of the domain.



var domain = require('domain');
var raygun = require('raygun');
var raygunClient = new raygun.Client().init({ apiKey: 'your API key' });
var server = require('http').createServer(function (req, res) {
 var d = domain.create();
 d.on('error', function (err) {
  raygunClient.send(err);
  // clean up and end
 });
 d.add(req);
 d.add(res);
 d.run(function () {
  // handle the req, res
 });
});
server.listen(3000);

Hopefully this will give you a better understanding of the error handling in ES42en.js using Raygun.

Keep cleaning up errors!


Related articles: