简体   繁体   English

如何记录未捕获的错误?

[英]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.

相关问题 如何在浏览器中隐藏未捕获的错误? - How can I hide an uncaught error from my browser? 我如何修复错误 Uncaught ReferenceError: addEvent is not defined - How can i fix the error Uncaught ReferenceError: addEvent is not defined 我如何解决 Uncaught SyntaxError: Unexpected token &#39;&lt;&#39; 错误 - How i can solve Uncaught SyntaxError: Unexpected token '<' error 如何修复此代码中未捕获的类型错误? - How can I fix the uncaught type error in this code? 我收到“未捕获的参考错误:未定义时刻”错误。 我怎样才能解决这个问题? - I am getting 'Uncaught Reference Error: moment is not defined' error. How can I fix this? 如何解决 react.js 中的“未捕获错误:找不到模块“路径”错误 - How can I solve 'Uncaught Error: Cannot find module 'path'' error in react.js 如何在摩卡中测试未捕获的错误? - How can I test uncaught errors in mocha? //未捕获的错误:选择颜色不是功能-我找不到错误 - //Uncaught Error: Pick color is not a function - I can not find the Error 未捕获的参考错误:..未定义..但我可以在错误字符串中看到变量 - Uncaught reference error: .. is not defined .. but I can see the variable in the error string 我无法弄清楚错误是什么,错误:未捕获的 ReferenceError - I can't figure out what the error is, Error: Uncaught ReferenceError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM