Q:then 回调中的静默异常?
Silent exceptions in Q:then callback?
我在 Q.promise 的 'then' 回调函数中调用 null 变量的函数有一些问题。
第一次调用(没有使用 Q)会显示错误,而第二次(没有使用 Q)则不会。
小例子:
var Q = require('q');
var nul = null;
var exp;
(function (exp) {
var A = (function () {
function A() {
};
A.prototype.foo = function () {
var d = Q.defer();
d.resolve('Hello, world');
return d.promise;
};
A.prototype.bar = function (i) {
switch (i) {
case 0:
/**
* That's all ok, "TypeError: Cannot read property 'qqq' of null"
*/
console.log(nul);
nul.qqq();
console.log('ok');
break;
case 1:
/**
* it's not ok, I see only result of "console.log(nul)", line 29
*/
this.foo().then(function () {
console.log(nul);
nul.qqq();
console.log('ok');
});
break;
};
};
return A;
})();
exp.A = A;
}) (exp || (exp = {}));
exp.a = new exp.A();
// You should run functions SEPARATELY!!!
exp.a.bar(0); // in that case: that's all ok, "TypeError: Cannot read property 'qqq' of null"
exp.a.bar(1); // int that case: it's not ok, I see only result of "console.log(nul)", line 29
我不知道怎么解决
您没有在控制台上看到第二个错误的原因是 Q 捕获所有错误并让您单独处理它们。
您可以通过链接 catch()
函数来处理 then()
中的错误,在您的示例中,这可以通过以下方式完成:
this.foo().then(function () {
console.log(nul);
nul.qqq();
console.log('ok');
}).catch(function(error) {
// do something with error
console.log(error);
});
您也可以通过在 then()
中使用 try/catch 块来获得此行为,如下所示:
this.foo().then(function () {
try {
console.log(nul);
nul.qqq();
console.log('ok');
} catch (e) {
console.log(e);
}
});
旧答案
这里有一些在 JS/node.js 中捕获错误的选项:
Try/Catch 块
它们的工作方式与它们的 Java/C# 等效,用 try
块包装您进行的每个调用并捕获错误,在 catch
块中处理它
try {
exp.a.bar(0);
} catch(e) {
console.log(e);
}
您还可以添加 finally 块,检查 exception/error 的类型等等,您可以在 MDN page
上阅读更多相关信息
Node.js uncaughtException 处理程序
在节点中,您可以捕获所有未捕获的错误,这将停止您的程序,方法是将回调绑定到 uncaughtException
事件,如下所示:
process.on('uncaughtException', function (e) {
console.log('Error: ' + e);
});
这并不总是在程序中做的最好的事情,但如果你真的不想停止执行,这是一个选择。
最后,我建议您查看此 official article 以获得有关处理 node.js
中错误的最佳实践
我在 Q.promise 的 'then' 回调函数中调用 null 变量的函数有一些问题。
第一次调用(没有使用 Q)会显示错误,而第二次(没有使用 Q)则不会。
小例子:
var Q = require('q');
var nul = null;
var exp;
(function (exp) {
var A = (function () {
function A() {
};
A.prototype.foo = function () {
var d = Q.defer();
d.resolve('Hello, world');
return d.promise;
};
A.prototype.bar = function (i) {
switch (i) {
case 0:
/**
* That's all ok, "TypeError: Cannot read property 'qqq' of null"
*/
console.log(nul);
nul.qqq();
console.log('ok');
break;
case 1:
/**
* it's not ok, I see only result of "console.log(nul)", line 29
*/
this.foo().then(function () {
console.log(nul);
nul.qqq();
console.log('ok');
});
break;
};
};
return A;
})();
exp.A = A;
}) (exp || (exp = {}));
exp.a = new exp.A();
// You should run functions SEPARATELY!!!
exp.a.bar(0); // in that case: that's all ok, "TypeError: Cannot read property 'qqq' of null"
exp.a.bar(1); // int that case: it's not ok, I see only result of "console.log(nul)", line 29
我不知道怎么解决
您没有在控制台上看到第二个错误的原因是 Q 捕获所有错误并让您单独处理它们。
您可以通过链接 catch()
函数来处理 then()
中的错误,在您的示例中,这可以通过以下方式完成:
this.foo().then(function () {
console.log(nul);
nul.qqq();
console.log('ok');
}).catch(function(error) {
// do something with error
console.log(error);
});
您也可以通过在 then()
中使用 try/catch 块来获得此行为,如下所示:
this.foo().then(function () {
try {
console.log(nul);
nul.qqq();
console.log('ok');
} catch (e) {
console.log(e);
}
});
旧答案
这里有一些在 JS/node.js 中捕获错误的选项:
Try/Catch 块
它们的工作方式与它们的 Java/C# 等效,用 try
块包装您进行的每个调用并捕获错误,在 catch
块中处理它
try {
exp.a.bar(0);
} catch(e) {
console.log(e);
}
您还可以添加 finally 块,检查 exception/error 的类型等等,您可以在 MDN page
上阅读更多相关信息Node.js uncaughtException 处理程序
在节点中,您可以捕获所有未捕获的错误,这将停止您的程序,方法是将回调绑定到 uncaughtException
事件,如下所示:
process.on('uncaughtException', function (e) {
console.log('Error: ' + e);
});
这并不总是在程序中做的最好的事情,但如果你真的不想停止执行,这是一个选择。
最后,我建议您查看此 official article 以获得有关处理 node.js
中错误的最佳实践