Node.js The custom error type detailed _node.js

Source: Internet
Author: User
Tags assert constructor

Objective

Generally speaking, very few people will consider how to deal with the error of the application of the strategy, the debugging process, the simple use of console.log(‘error') positioning errors, basically enough, by leaving these debugging information, can for our later debugging process to rise a lot of time, improve the maintenance. So it's very important that the error tip. At the same time, it will bring some bad usage. The recent project has used the custom error type, feels the need to have a thorough understanding, therefore has written this article, is convenient for oneself and needs everybody to consult when needs.

Subclassing Error

First we can define a subclass of the Error. Through Object.create and util.inherits easy to implement:

var assert = require (' assert ');
var util = require (' util ');

function NotFound (msg) {
 error.call (this);
 This.message = msg;
}
Util.inherits (NotFound, Error);
var error = new NotFound (' not found ');
ASSERT (Error.message);
ASSERT (Error instanceof NotFound);
ASSERT (Error instanceof error);
Assert.equal (Error instanceof Rangeerror, false);

You can instanceof check for the type of error, depending on the type, for different processing.

The above code sets the band and message error is NotFound Error An instance of the and, but not RangeError .

If you use a express frame, you can set the other to properties error become more useful.

For example, when dealing with an HTTP error, it can be written like this:

function NotFound (msg) {
 error.call (this);
 This.message = msg;
 This.statuscode = 404;
}

Error messages can now be handled through error-handling middleware:

App.use (function (err, req, res, next) {
 console.error (err.stack);

 if (!err.statuscode | | err.statuscode = = =) {
 Emails.error ({err:err, req:req});
 }

 Res.send (Err.statuscode, err.message);

This sends the HTTP status code to the browser, which, when err statusCode not set or equal to 500, sends the error via email. This will eliminate the mistakes of 404, 401, 403, and so on.

Reading console.error(err.stack) does not actually work as expected, like node, where Chrome is based on the V8 's available Error.captureStackTrace(this, arguments.callee) error constructor for stack tracking.

var notfound = function (msg) {
 error.call (this);
 Error.capturestacktrace (this, arguments.callee);
 This.message = MSG | | ' Not Found ';
 This.statuscode = 404;
 THIS.name = "NotFound"
}
util.inherits (NotFound, Error);

Export. Notfounderror = NotFound;

Of course, we can also extend the abstract error type created above to other custom errors:

var notfounterror = require ('./error '). Notfounterror; 
var usernotfound = function (msg) {
 this.constructor.super_ (msg);
}

Util.inherits (Usernotfound, notfounderror);

Summarize

The above is node.js the whole content of custom error type, hope this article content to everybody study or use node.js can have certain help, if have the question everybody can message exchange. Thank you for your support to the cloud-dwelling community.

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.