无法捕获子进程执行回调函数的错误

leo*_*org 0 javascript exec node.js restify typescript

语境

我想在我的网站后端执行命令行调用。如果此命令行调用失败,我想捕获错误并引发新的自定义错误。

我尝试抛出错误如下:

async function someFunction(): Promise<void> {
  ...
  const result = exec(`some command`);

  result.on('error', (error) => {
    console.log(error.message);
    throw error;
  });
}
Run Code Online (Sandbox Code Playgroud)

async function someFunction(): Promise<void> {
  ...
  exec(`some command`, (error) => {
    if (error) {
      throw error;
    }
  });
}
Run Code Online (Sandbox Code Playgroud)

并像这样抓住它:

try {
  await someFunction();
} catch (e) {
  ...
  throw new Error('Meaningfull error')
}
Run Code Online (Sandbox Code Playgroud)

问题

但代码永远不会到达 catch 块,因为它会在我到达抛出错误时关闭。

Error: Command failed: some Command: Kommando nicht gefunden. //(Command not found)

    at ChildProcess.exithandler (node:child_process:398:12)
    at ChildProcess.emit (node:events:527:28)
    at ChildProcess.emit (node:domain:475:12)
    at maybeClose (node:internal/child_process:1092:16)
    at Socket.<anonymous> (node:internal/child_process:451:11)
    at Socket.emit (node:events:527:28)
    at Socket.emit (node:domain:475:12)
    at Pipe.<anonymous> (node:net:709:12) {
  code: 127,
  killed: false,
  signal: null,
  cmd: 'some Command'
}
Waiting for the debugger to disconnect...
[nodemon] app crashed - waiting for file changes before starting...
Run Code Online (Sandbox Code Playgroud)

我尝试删除错误处理尝试,并且该应用程序继续关注自己的业务。我不明白为什么当我尝试处理错误时它总是崩溃......

我知道命令失败了,但我不在乎。我只是希望能够处理该错误。

编辑1

我还尝试了 exec 调用周围的 try catch 。

Error: Command failed: some Command: Kommando nicht gefunden. //(Command not found)

    at ChildProcess.exithandler (node:child_process:398:12)
    at ChildProcess.emit (node:events:527:28)
    at ChildProcess.emit (node:domain:475:12)
    at maybeClose (node:internal/child_process:1092:16)
    at Socket.<anonymous> (node:internal/child_process:451:11)
    at Socket.emit (node:events:527:28)
    at Socket.emit (node:domain:475:12)
    at Pipe.<anonymous> (node:net:709:12) {
  code: 127,
  killed: false,
  signal: null,
  cmd: 'some Command'
}
Waiting for the debugger to disconnect...
[nodemon] app crashed - waiting for file changes before starting...
Run Code Online (Sandbox Code Playgroud)

但遗憾的是,应用程序在抛出错误行时崩溃了。

编辑2

我使用基于 Restify 中间件的错误处理程序

    try {
      exec(`some Command`, (error) => {
        if (error) {
          throw error;
        }
      });
    } catch (e) {
      throw e;
    }
Run Code Online (Sandbox Code Playgroud)

举个例子。以下代码正在按预期进行处理:

this.restify.on('uncaughtException', function(req, res, route, err) {
  res.send(500, err.message);
});
Run Code Online (Sandbox Code Playgroud)

我还尝试在每个捕获中添加 console.log(error) 。没有帮助。

小智 5

您好,我认为解决此问题的正确方法是将其包装到 Promise 中,因为它是回调中的错误,如果您实际上想尝试捕获发生错误的位置,则必须在回调内完成据我所知。

可能的解决方案:

function someFunction() {
    return new Promise((resolve, reject) => {
        exec(`some command`, (error) => {
            if (error) {
                reject(error);
            }
            resolve("coding is fun ");
        });
    })
}

async function main() {
    try {
        await someFunction();
    } catch (e) {
        console.error(e)
        throw e;
    }
}

Run Code Online (Sandbox Code Playgroud)

  • 你无法想象我花了多长时间才让这个项目运行起来。非常感谢您&lt;3 说实话,我对您的感谢实在是不够。“编码很有趣”...是的,当然 xD (2认同)