带有 fs/promises 和 async..await 的堆栈跟踪
我有一个类似于这个问题 并在此问题中进行了描述。当与 async..await 一起使用时,来自 fs/promises
的错误中没有堆栈跟踪,并且无法确定错误发生的位置。
给定应用程序:
foo.js
const { bar } = require('./bar');
async function foo() {
await bar();
}
(async () => {
await foo();
})().catch(console.error);
bar.js
const fs = require('fs/promises');
exports.bar = async function bar() {
await fs.readFile('some');
}
当它像 node foo.js
一样运行时,这会导致没有堆栈跟踪的错误:
[Error: ENOENT: no such file or directory, open '...\some'] {
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: '...\\some'
}
其中 error.stack
包含'ENOENT:没有这样的文件或目录,打开'...\some'。
当它像 node --stack_trace_limit=100 -r trace foo.js
和 trace
包一样运行时,就像链接源中建议的那样,这会导致以下堆栈跟踪:
请注意,内部条目呈灰色,可以过滤掉。
这个带有 trace
和 clarify
包的 node --stack_trace_limit=100 -r trace -r recognize foo.js
会产生以下输出:
Error: ENOENT: no such file or directory, open '...\some'
at ...\foo.js:8:2
at Object.<anonymous> (...\foo.js:10:3) {
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: '...\\some',
[Symbol(originalCallSite)]: [],
[Symbol(mutatedCallSite)]: [ CallSite {}, CallSite {} ]
}
问题是所有输出中均未提及 bar.js。
我的目的是提供干净的错误输出,没有内部条目以及错误发生的确切位置,即 bar.js 中的行号。
这个问题有哪些可能的解决方案?
I have a problem that is similar to this one and described in this issue. There are no stack traces in errors that come from fs/promises
when used with async..await, and no way to determine where the error happened.
Given the app:
foo.js
const { bar } = require('./bar');
async function foo() {
await bar();
}
(async () => {
await foo();
})().catch(console.error);
bar.js
const fs = require('fs/promises');
exports.bar = async function bar() {
await fs.readFile('some');
}
When it runs like node foo.js
, this results in an error with no stack trace:
[Error: ENOENT: no such file or directory, open '...\some'] {
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: '...\\some'
}
Where error.stack
contains 'ENOENT: no such file or directory, open '...\some'.
When it runs like node --stack_trace_limit=100 -r trace foo.js
with trace
package, like it's suggested in linked sources, this results in this stack trace:
Notice that internal entries are grayed and can be filtered out.
This node --stack_trace_limit=100 -r trace -r clarify foo.js
with trace
and clarify
packages results in this output:
Error: ENOENT: no such file or directory, open '...\some'
at ...\foo.js:8:2
at Object.<anonymous> (...\foo.js:10:3) {
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: '...\\some',
[Symbol(originalCallSite)]: [],
[Symbol(mutatedCallSite)]: [ CallSite {}, CallSite {} ]
}
The problem is that bar.js isn't mentioned in all of the outputs.
My intention is to provide clean error output with no internal entries and the exact location where the error occurs, i.e. line number in bar.js.
What are possible solutions to this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于问题的根本原因是
fs/promises
Error
在设计上没有堆栈,为什么当您需要堆栈详细信息时不抛出自己的堆栈呢?除此之外,我建议避免使用 Promise 的
catch
方法,而仅使用 async/await 构造。foo.js
bar.js
这给出了以下结果:
可能您不需要使用
trace
和/或clarify
,但是当然您仍然可以根据需要自由地使用它们来更改堆栈跟踪。Since the root cause of your problem is that
fs/promises
Error
s have no stack by design, why simply not throwing your own when you need stack details?More than this I can suggest to avoid using the
catch
method of promises and to use only the async/await construct.foo.js
bar.js
This gives following result:
Probably you neither need to use
trace
and/orclarify
, but of course you are still free to use them to alter the stack trace at your needs.如此接近:)
编辑:
基于 fs/promise 问题,
这是一个已知问题,可以根据需要解决,如下所示。
除了使用跟踪/澄清之外,
直接在 fs.readFile 调用(bar.js)上尝试/捕获
您也可以尝试同步读取文件。
So close :)
Edit:
based on fs/promise issue,
it's a known issue, and could be worked-around on demand, as suggested below.
In addition for using trace/clarify,
do try/catch directly on fs.readFile call (bar.js)
You can also try sync read file.
一种可能的解决方案是使用 Node.js >= v21.2.0。 Node.js 版本 v21.2.0 包含 提交 来自 PR 49849 将堆栈跟踪添加到
node:fs/promises
。ES 模块和顶层等待着您。
package.json
bar.js
foo.js
现在运行
node foo.js
生成堆栈跟踪:ES具有异步功能的模块(无顶级等待)。
foo.js
运行
node foo.js
后会产生类似的堆栈跟踪:CommonJS
package.json
bar.js
:
node foo.js
后One possible solution is to use Node.js >= v21.2.0. Node.js release v21.2.0 included a commit from PR 49849 that adds stack traces to
node:fs/promises
.ES modules and top level await.
package.json
bar.js
foo.js
Now run
node foo.js
to generate the stack trace:ES modules with async function (no top level await).
foo.js
Which after running
node foo.js
results in a similar stack trace:CommonJS
package.json
bar.js
foo.js
After running
node foo.js
: