JavaScript rethrowing an Exception preserving the stack trace

后端 未结 3 838
生来不讨喜
生来不讨喜 2021-01-19 12:17

In Chrome, when an exception occurs, it prints a stack trace to the console log. This is extremely useful, but unfortunately in cases where an exception has been rethrown th

相关标签:
3条回答
  • 2021-01-19 12:32

    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);
    }
    
    0 讨论(0)
  • 2021-01-19 12:40

    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;
    }
    
    0 讨论(0)
  • 2021-01-19 12:45

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

    0 讨论(0)
提交回复
热议问题