Error.captureStackTrace(targetObject[, constructorOpt])
targetObject<Object>constructorOpt<Function>
在 targetObject 上創建一個 .stack 屬性,訪問該屬性時會返回一個字符串,該字符串表示代碼中調用 Error.captureStackTrace() 的位置。
const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack;  // Similar to `new Error().stack`
跟蹤的第一行將以 ${myObject.name}: ${myObject.message} 為前綴。
可選的 constructorOpt 參數接受一個函數。如果給定,所有高於 constructorOpt 的幀,包括 constructorOpt ,都將從生成的堆棧跟蹤中省略。
constructorOpt 參數對於向用戶隱藏錯誤生成的實現細節很有用。例如:
function MyError() {
  Error.captureStackTrace(this, MyError);
}
// Without passing MyError to captureStackTrace, the MyError
// frame would show up in the .stack property. By passing
// the constructor, we omit that frame, and retain all frames below it.
new MyError().stack;
相關用法
- Node.js Error.message用法及代碼示例
 - Node.js Error.stack用法及代碼示例
 - Node.js EventEmitter用法及代碼示例
 - Node.js ECDH.convertKey(key, curve[, inputEncoding[, outputEncoding[, format]]])用法及代碼示例
 - Node.js ECDH用法及代碼示例
 - Node.js EventTarget.addEventListener(type, listener[, options])用法及代碼示例
 - Node.js ECDH.setPublicKey(publicKey[, encoding])用法及代碼示例
 - Node.js ServerHttp2Stream http2stream.pushStream(headers[, options], callback)用法及代碼示例
 - Node.js http2.Http2ServerRequest request.url用法及代碼示例
 - Node.js request.socket用法及代碼示例
 - Node.js assert.notEqual(actual, expected[, message])用法及代碼示例
 - Node.js tlsSocket.authorized用法及代碼示例
 - Node.js zlib.deflateRaw()用法及代碼示例
 - Node.js http.IncomingMessage message.rawHeaders用法及代碼示例
 - Node.js Console用法及代碼示例
 - Node.js GM transparent()用法及代碼示例
 - Node.js URL.protocol用法及代碼示例
 - Node.js http.Agent.reuseSocket(socket, request)用法及代碼示例
 - Node.js fs.filehandle.datasync()用法及代碼示例
 - Node.js socket.bind()用法及代碼示例
 - Node.js v8.getHeapSpaceStatistics()用法及代碼示例
 - Node.js http2session.destroyed用法及代碼示例
 - Node.js http.ServerResponse response.statusCode用法及代碼示例
 - Node.js Buffer buf.writeBigUInt64BE(value[, offset])用法及代碼示例
 - Node.js Http2ServerResponse.finished用法及代碼示例
 
注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 Error.captureStackTrace(targetObject[, constructorOpt])。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
