How to Use Raygun to handle errors in Node. js applications

Source: Internet
Author: User

How to Use Raygun to handle errors in Node. js applications

This article mainly introduces how to use Raygun to handle errors in Node. js applications. Node. js is a JavaScript framework for servers. For more information, see

 

 

Our raygun4node package provides a convenient way to send your Node. js error to Raygun. It can be easily installed using npm:

?

1

Npm install raygun

It can provide you with a raygun client, you can use it to configure your API key, and can be used to manually send error messages. but later you may say, "I don't want to manually send all the errors to Raygun. It sounds like there is a lot of work to do!" If you are using express. js, you can easily solve this worry with the express processor.

?

1

2

3

Var raygun = require ('raygun ');

Var raygunClient = new raygun. Client (). init ({apiKey: 'Your API key '});

App. use (raygunClient. expressHandler );

In other cases, you may just want to use this method to listen for uncaught exceptions and send error messages.

?

1

2

3

4

5

Var raygun = require ('raygun ');

Var raygunClient = new raygun. Client (). init ({apiKey: 'Your API key '});

Process. on ('uncaughtexception ', function (err ){

RaygunClient. send (err );

});

If you plan to start doing so, you must understand its meaning. however, when a time bubble keeps returning to the event loop, this event will be triggered. if you add a listener for this event, the default action will not happen again. when the default action is performed, the call stack information is printed and the process is exited. if you continue after triggering this operation, your node process will be in an unspecified state. node. the js document specifically mentions that you should not use this item, and it may be removed in the future. the recommended alternative is to use the domain domains. the following is a simple and small example. You can see how the raygun client is applicable to your domain.

 

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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 );

I hope this will help you better understand how to handle errors in Node. js of Raygun.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.