Chrome: Print exception details to console

大城市里の小女人 提交于 2019-12-12 11:06:15

问题


How do I print the stack trace of an Exception in the chrome devtools from my code?

I tried the following:

 function doSomething() { 
     undefined(); // This throws an exception
 }

 try {
      doSomething();
 } catch (e) {
      console.error("Exception thrown", e);
 }

But this yields the following result:

 Exception thrown TypeError {}

And if I expand the arrow next to it, it points me to the line where the console.error() call was made, so I don't get to see where the original error actually happened.

What would be the best way to include the original error information (including message and complete stack trace to the exact location where the error happened) in the console output?


回答1:


Object Error has a property stack. Print it out.

console.error("Exception thrown", e.stack);

Please note that stack property is not standardized and it is only used by V8 based browsers + IE. Firefox uses different convention.




回答2:


You can output the error as object

console.error("%O", e)

Using string substitutions



来源:https://stackoverflow.com/questions/19036121/chrome-print-exception-details-to-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!