在throw()生成的node.js上缺少堆栈跟踪uncaughtException [英] Missing stack trace on node.js uncaughtException generated by throw()

查看:124
本文介绍了在throw()生成的node.js上缺少堆栈跟踪uncaughtException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试捕获node.js uncaughtException的堆栈跟踪,它适用于不同的错误但不适用于throw()语句:

I'm trying to catch the stack trace of an node.js uncaughtException and it works fine for different errors but not for throw() statements:

正确的堆栈跟踪异常处理:

Correct stack trace on exception handling:

$ cat errorFunc.js 
process.on('uncaughtException', function(exception) {
    console.log('uncaughtException occurred: ' + exception.stack);
});
MyError();

$ node errorFunc.js 
    uncaughtException occurred: ReferenceError: MyError is not defined
    at Object.<anonymous> (/home/jolcese/code/WebEnclaves/Server/errorFunc.js:5:1)
    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 Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3
$

由throw()引起的异常缺少堆栈跟踪:

Missing stack trace on exception caused by throw():

$ cat errorThrow.js 
process.on('uncaughtException', function(exception) {
    console.log('uncaughtException occurred: ' + exception.stack);
});
throw('my error');

$ node errorThrow.js 
uncaughtException occurred: undefined
$

知道为什么吗?

谢谢
Jose

Thanks Jose

免责声明:我知道使用process.on('uncaughtException')是一件非常非常糟糕的事情,我将受到惩罚,但在此代码中使用域名不是一个选项。

Disclaimer: I know that using process.on('uncaughtException') is a very, very bad thing and I will be punished but using domains is not an option in this code.

推荐答案

JavaScript允许你抛出任何东西。

JavaScript lets you throw anything.

如果你想在JavaScript中抛出堆栈跟踪错误,你需要抛出错误对象。
(规范)

If you want to throw errors with stack traces in JavaScript, you need to throw Error objects. (specification )

此外,抛出是一个运算符而不是函数。

Also, throw is an operator and not a function.

尝试

throw new Error('my error');

参见手册

这篇关于在throw()生成的node.js上缺少堆栈跟踪uncaughtException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆