Rod*_*igo 5 javascript exec node.js
我execSync用来运行soffice命令。我遇到的问题是引发错误时,execSync只是将错误记录到控制台,无法捕获它。我尝试使用一条try catch语句,但它仍然只是将错误记录到控制台。
function convertToPdf(filepath, destpath) {
var cmd = 'sofice command';
try {
var res = execSync(cmd, { encoding: 'utf8' });
} catch (e) {
console.log("Errors:", e);
}
console.log("res:", res);
}
convertToPdf("test.docx");
Run Code Online (Sandbox Code Playgroud)
我运行并返回:
Error: source file could not be loaded
res:
Run Code Online (Sandbox Code Playgroud)
请注意,即使明显抛出错误,我的catch语句也永远不会被记录,但是Error:由于我没有记录错误,因此会自动记录另一条消息。
小智 1
尝试这个:
function myExecSync(command, trim = true, cwd = pwd, opts = {}) {
const ret = execSync(command, { cwd, ...opts }).toString('utf-8');
return trim ? ret.trim() : ret;
}
Run Code Online (Sandbox Code Playgroud)
并手动写入 stdio: 'pipe' 以防止 childprocess.stderr 输出到控制台。喜欢
myExecSync(command, true, cwd, { stdio: 'pipe' })
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2048 次 |
| 最近记录: |