(feat) gracefully handle invalid commands (#79)

* handle invalid commands
* better test
* format
This commit is contained in:
easong-openai
2025-04-16 12:30:43 -07:00
committed by GitHub
parent a5641a0d9c
commit 75e2454d1d
2 changed files with 76 additions and 7 deletions

View File

@@ -122,13 +122,16 @@ export function exec(
abortSignal.addEventListener("abort", abortHandler, { once: true });
}
}
if (!child.pid) {
return Promise.resolve({
stdout: "",
stderr: `likely failed because ${prog} could not be found`,
exitCode: 1,
});
}
// If spawning the child failed (e.g. the executable could not be found)
// `child.pid` will be undefined *and* an `error` event will be emitted on
// the ChildProcess instance. We intentionally do **not** bail out early
// here. Returning prematurely would leave the `error` event without a
// listener which in Node.js results in an "Unhandled 'error' event"
// processlevel exception that crashes the CLI. Instead we continue with
// the normal promise flow below where we are guaranteed to attach both the
// `error` and `exit` handlers right away. Either of those callbacks will
// resolve the promise and translate the failure into a regular
// ExecResult object so the rest of the agent loop can carry on gracefully.
const stdoutChunks: Array<Buffer> = [];
const stderrChunks: Array<Buffer> = [];