Node.js下自定义错误类型详解

2020-06-17 06:57:42易采站长站整理
事实上并不会像预期那样工作,像 node, chrome 基于 V8 的可以使用
Error.captureStackTrace(this, arguments.callee) 
的错误构造函数来进行堆栈跟踪。


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;

当然我们还可以将上面这个创建的抽象错误类型扩展到其他自定义错误中:


var notFountError = require('./error').NotFountError;
var UserNotFound = function(msg){
this.constructor.super_(msg);
}

util.inherits(UserNotFound, notFoundError);

总结

以上就是Node.js下自定义错误类型的全部内容,希望本文的内容对大家学习或者使用Node.js能有一定的帮助,如果有疑问大家可以留言交流。谢谢大家对软件开发网的支持。