JavaScript rethrowing an Exception preserving the stack trace

时间秒杀一切 提交于 2019-12-01 22:01:40

This is a known bug in Chrome, and unfortunately there's no workaround that I'm aware of.

Philip Taylor

The best you can do is grab the original stack and print it. I use this in unit testing tools.

try{
  ...
}
catch(e){
    console.log(e.stack);
    console.log(e.message);
    throw(e);
}

In my situation console.log() was not possible. What I did is:

}catch( error ){
 // 'throw' replaces the stack trace.
 // To preserve the stack add it to the message.
 error.message += '; Stack trace: ' + error.stack;
 throw error;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!