简体   繁体   English

如何使用 Node 和 TypeScript 获取异步堆栈跟踪?

[英]How can I get async stack traces with Node and TypeScript?

I wish I could get stack traces to work in TypeScript.我希望我可以在 TypeScript 中获得堆栈跟踪。 I only seem to see the bottommost function name.我似乎只看到最底层的函数名称。 I'm using Node.js v12.4.0 on Windows 10 (1803).我在 Windows 10 (1803) 上使用 Node.js v12.4.0。

This is the code:这是代码:

async function thrower() {
  throw new Error("test");
}

async function level1() {
  return await thrower();
}

async function level2() {
  return await level1();
}

async function level3() {
  return await level2();
}

async function main() {
  try {
    await level3();
  } catch(err) {
    console.warn("main error", err);
  }
}

console.log("node version", process.version);

main().then(() => {
  console.log("all done " + __filename);
}).catch((err) => {
  console.error("Something went wrong in here :(", __filename, err);
})

The resulting stack trace that does not mention level1 or level2 or level3 :生成的堆栈跟踪未提及level1level2level3

ts-node test-stack.ts
node version v12.4.0
main error Error: test
    at D:\dev\server\test-stack.ts:2:9
    at step (D:\dev\server\test-stack.ts:31:23)
    at Object.next (D:\dev\server\test-stack.ts:12:53)
    at D:\dev\server\test-stack.ts:6:71
    at new Promise (<anonymous>)
    at __awaiter (D:\dev\server\test-stack.ts:2:12)
    at thrower (D:\dev\server\test-stack.ts:37:12)
    at D:\dev\server\test-stack.ts:6:16
    at step (D:\dev\server\test-stack.ts:31:23)
    at Object.next (D:\dev\server\test-stack.ts:12:53)
all done D:\dev\server\test-stack.ts

After some research and noticing the __awaiter , I decided to inspect what's TypeScript targeting.经过一些研究并注意到__awaiter ,我决定检查什么是 TypeScript 目标。 That was my problem.那是我的问题。

Here's my bad tsconfig.json :这是我不好的tsconfig.json

{
    "compilerOptions": {
      "experimentalDecorators": true,
      "emitDecoratorMetadata": true,
      "downlevelIteration": true,
    },
    "include": [
        "server/**/*", "tests"
    ],
    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ]
}

And this "target": "es2018" fixed it:而这个"target": "es2018"修复了它:

{
    "compilerOptions": {
      "experimentalDecorators": true,
      "emitDecoratorMetadata": true,
      "downlevelIteration": true,
      "target": "es2018"
    },
    "include": [
        "server/**/*", "tests"
    ],
    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ]
}

Resulting in this stack trace:导致此堆栈跟踪:

ts-node test-stack.ts
node version v12.4.0
main error Error: test
    at thrower (D:\dev\server\test-stack.ts:2:9)
    at level1 (D:\dev\server\test-stack.ts:6:16)
    at level2 (D:\dev\server\test-stack.ts:10:16)
    at level3 (D:\dev\server\test-stack.ts:14:16)
    at main (D:\dev\server\test-stack.ts:19:11)
    at Object.<anonymous> (D:\dev\server\test-stack.ts:27:1)
    at Module._compile (internal/modules/cjs/loader.js:774:30)
    at Module.m._compile (C:\Users\yuv\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:439:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:785:10)
    at Object.require.extensions.<computed> [as .ts] (C:\Users\yuv\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:442:12)
all done D:\dev\server\test-stack.ts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以在 Node 7 中使用 --harmony_async_await 跨异步/等待边界获取堆栈跟踪? - Is it possible to get stack traces across async/await boundaries using --harmony_async_await in Node 7? 如何在浏览器中获取 XHR 调用的堆栈跟踪? - How to get stack traces for XHR calls in a browser? 节点 16+ 中的 --async-stack-traces 发生了什么,是否有新的替代方案? - What happened to --async-stack-traces in node 16+ and is there a new alternative? Node.JS + TypeScript 如何从堆栈跟踪中跟踪源代码? - Node.JS + TypeScript how I can track the source code from the stack trace? 如何获取源映射以提供正确的堆栈跟踪? - How to get source maps to provide correct stack traces? 如何使用mocha和Babel在SyntaxErrors上获得更短的堆栈跟踪? - How to get shorter stack traces on SyntaxErrors with mocha and Babel? 保留node.js中的堆栈跟踪 - Preserve stack traces in node.js 如何使用 TypeScript 运行顶级异步/等待 function? - How can I run a top level async / await function with TypeScript? 使用async / await时如何获取完整的Node.js堆栈跟踪? - How to get the full Node.js stack trace when using async/await? 如何使用Node / Typescript获得签名的S3 URL? - How can I get signed S3 url using Node/Typescript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM