gpt4 book ai didi

node.js - 在 NodeJs 中使用 child_process.execSync 进行错误处理

转载 作者:行者123 更新时间:2023-12-03 08:04:10 31 4
gpt4 key购买 nike

这是我的 NodeJs 片段:

const {execSync} = require('child_process');
testoutput = execSync('make test');

如果 make 进程完全退出,我将从 execSync 进程获取 stdout 作为输出到 testoutput 变量。但是,如果 make test 过程遇到 stderr 错误,那么我会在 NodeJs 中收到错误。

如果 make 进程未退出并将 stdout 存储在变量 testoutput 中,如何将 stderr 消息存储到输出变量 testoutput>testoutput 如果进程正常退出并带有 stdout 输出。

最佳答案

要使用 execSync() 捕获错误,您需要在调用周围使用 try/catch,因为错误会引发异常。

关于node.js - 在 NodeJs 中使用 child_process.execSync 进行错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72946118/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com