节点 JS 未处理的错误事件
Node JS Unhandler Error Event
我是这里的新手,我正在尝试编写一个代码,其中使用了节点的事件发射器,请参见下面的代码,
var EventEmitter = require('events').EventEmitter;
var errors = require('./errors');
var defaults ={
default_api_endpoint_v0 : "someapi",
oauth_c_key: "oauth_consumer_key",
options_check: ['oauth_consumer_key']
};
var someapi = function(options){
this.options = options;
EventEmitter.call(this);
if(!this.options)
this.emit('error',errors.options_not_passed);
return;
//return errors.options_not_passed
if(!this.checkOptions()){
return errors.options_passed_not_satisfied
}
};
someapi.prototype = Object.create(EventEmitter.prototype);
someapi.prototype.checkOptions = function(){
var thatOptions = this.options;
var bool = false;
if(typeof thatOptions !== 'object')
return errors.options_passed_not_satisfied;
Object.keys(thatOptions).forEach(function(key){
Object.keys(defaults.options_check).forEach(function(Key){
if(key === defaults.options_check[Key]){
bool = true;
}
})
});
return bool;
};
module.exports = someapi
我在下面这样调用它,
var op = new someapi();
op.on('error',function(err){
console.log("Emitted" + err);
})
但是它抛出这个错误,我不知道我做错了什么,
events.js:72
throw er; // Unhandled 'error' event
^
Error: Please pass the options
at Object.<anonymous> (d:\gitHub\someapi\lib\errors.js:2:26)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (d:\gitHub\someapi\lib\api.js:2:14)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
在上面的错误中,请传递我返回的选项,它位于错误文件中,见下文,
module.exports = {
options_not_passed : new Error("Please pass the options"),
options_passed_not_satisfied : "Please check are you sending all the options_params"
};
谁能给我一些意见,我在这里做错了什么???
对于上述内容,我知道我在为 someapi class 创建对象时没有传递选项对象。我想知道为什么我的错误没有在 "on" 事件中得到处理。
P.S在此先感谢。
原因是因为您从构造函数发出错误。在创建实例时 无法附加侦听器(或对对象执行任何其他操作)(因为您没有对新对象的引用尚未从 new someapi()
返回。
我是这里的新手,我正在尝试编写一个代码,其中使用了节点的事件发射器,请参见下面的代码,
var EventEmitter = require('events').EventEmitter;
var errors = require('./errors');
var defaults ={
default_api_endpoint_v0 : "someapi",
oauth_c_key: "oauth_consumer_key",
options_check: ['oauth_consumer_key']
};
var someapi = function(options){
this.options = options;
EventEmitter.call(this);
if(!this.options)
this.emit('error',errors.options_not_passed);
return;
//return errors.options_not_passed
if(!this.checkOptions()){
return errors.options_passed_not_satisfied
}
};
someapi.prototype = Object.create(EventEmitter.prototype);
someapi.prototype.checkOptions = function(){
var thatOptions = this.options;
var bool = false;
if(typeof thatOptions !== 'object')
return errors.options_passed_not_satisfied;
Object.keys(thatOptions).forEach(function(key){
Object.keys(defaults.options_check).forEach(function(Key){
if(key === defaults.options_check[Key]){
bool = true;
}
})
});
return bool;
};
module.exports = someapi
我在下面这样调用它,
var op = new someapi();
op.on('error',function(err){
console.log("Emitted" + err);
})
但是它抛出这个错误,我不知道我做错了什么,
events.js:72
throw er; // Unhandled 'error' event
^
Error: Please pass the options
at Object.<anonymous> (d:\gitHub\someapi\lib\errors.js:2:26)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (d:\gitHub\someapi\lib\api.js:2:14)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
在上面的错误中,请传递我返回的选项,它位于错误文件中,见下文,
module.exports = {
options_not_passed : new Error("Please pass the options"),
options_passed_not_satisfied : "Please check are you sending all the options_params"
};
谁能给我一些意见,我在这里做错了什么???
对于上述内容,我知道我在为 someapi class 创建对象时没有传递选项对象。我想知道为什么我的错误没有在 "on" 事件中得到处理。
P.S在此先感谢。
原因是因为您从构造函数发出错误。在创建实例时 无法附加侦听器(或对对象执行任何其他操作)(因为您没有对新对象的引用尚未从 new someapi()
返回。