JavaScript rethrowing an Exception preserving the stack trace

后端 未结 3 869
生来不讨喜
生来不讨喜 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: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;
    }
    

提交回复
热议问题