Error.captureStackTrace / Error.prepareStackTrace


// error.toString()(包含 error.name 和 error.message)
// 错误栈本不支持异步,chrome debugger 能支持异步(Long Stack Trace,v8 7.3 默认支持)


隐藏内部实现细节,优化错误栈

Error.captureStackTrace(targetObject[, constructorOpt])

在 targetObject 中添加一个 stack 属性。对该属性进行访问时,将以字符串的形式返回Error.captureStackTrace() 语句被调用时的代码位置信息(即:调用栈历史)。
当传递 constructorOpt 时,调用栈中所有 constructorOpt 函数之上的信息(包括 constructorOpt 函数自身),都会在访问 targetObject.stack 时被忽略。

自定义错误栈(这是全局的,可以在执行操作后还原)
Error.prepareStackTrace = function (error, callSites) {
return error.toString() + '\n' + callSites.map(callSite => {
return ' at ' + callSite.getFunctionName() + ' ('
+ callSite.getFileName() + ':'
+ callSite.getLineNumber() + ':'
+ callSite.getColumnNumber() + ')'
}).join('\n')
}
Unlike Java where the stack trace of an exception is a structured value that allows inspection of the stack state, the stack property in V8 just holds a flat string containing the formatted stack trace. This is for no other reason than compatibility with other browsers. However, this is not hardcoded but only the default behavior and can be overridden by user scripts.
For efficiency stack traces are not formatted when they are captured but on demand, the first time the stack property is accessed (?). A stack trace is formatted by calling