[英]How can I log an error as if it was uncaught?
I have a complex JavaScript application where when certain unexpected errors occur, I want to log them for debugging but continue. 我有一个复杂的JavaScript应用程序,当发生某些意外错误时,我想记录它们进行调试,然后继续。
How do I log them to the console so that their stack is accessible in the normal fashion for an uncaught error? 如何将它们记录到控制台, 以便以正常方式访问它们的堆栈以防出现未捕获的错误?
A uncaught error in the Chrome console normally shows only the error message, and has a disclosure triangle which can be clicked to show the stack trace. Chrome控制台中的未捕获错误通常仅显示错误消息,并且具有显示三角形,可以单击该三角形以显示堆栈跟踪。 This (and any other debugging features Chrome has now or might add in the future for uncaught errors) is what I want.
这就是我想要的(以及Chrome现在具有的任何其他调试功能,或者将来可能会出现的未捕获错误而添加的其他功能)。
If I do the obvious catch (e) { console.log(e); }
如果我做了明显的
catch (e) { console.log(e); }
catch (e) { console.log(e); }
or console.error(e)
, the error object is instead shown in object-inspector fashion — there is a triangle but clicking it shows the properties of the error object (so I have to click four more times to get the stack property in a poorly-formatted way). catch (e) { console.log(e); }
或console.error(e)
,而是以对象检查器的方式显示错误对象-有一个三角形,但是单击它可以显示错误对象的属性 (因此,我必须再单击四次才能获得堆栈属性格式不良)。 I could log e.stack
, but that's still missing some standard features. 我可以登录
e.stack
,但是仍然缺少一些标准功能。
setTimeout(function () { throw e; }, 0);
is another option, but confuses debugging because it changes the ordering of messages in the console. 是另一种选择,但由于它会更改控制台中消息的顺序,因此使调试感到困惑。 (Putting the entire code-which-might-throw in a setTimeout might be feasible, but I want to investigate just fixing the logging first.)
(将整个可能抛出的代码放入setTimeout中可能是可行的,但我想研究一下仅先修复日志记录。)
console.log(prettyformatstack(e));
instead! 代替!
where prettyformatstack(e)
takes an error returns the string you are looking for. 如果
prettyformatstack(e)
出错,则返回您要查找的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.