check / check (push) Waiting to run
Node's `fetch` rejects every failure below HTTP as `fetch failed` and puts the reason on `cause`. `describeError` in `src/errors.ts` gives the message followed by the messages on the cause chain, and for an `AggregateError`, which Node gives when every address of a host fails, the message of each address. `run` prints it, and the backup logs and records it. `classify` reads that text and calls a failure transient when `hasTransportCode`, now exported by `src/retry.ts`, finds a transport errno on the chain. A failed ML data fetch keeps its error as the cause. The bounded walk along the chain moved from `src/retry.ts` to `src/errors.ts`. A truncated download's message leaves the authentication failure to its cause. Model: opus-5-5
48 lines
1.7 KiB
TypeScript
48 lines
1.7 KiB
TypeScript
// Runs one CLI command for `bin/quak.ts` and exits with its code.
|
|
|
|
import type { Writable } from "node:stream";
|
|
import { ApiError } from "./api/client.js";
|
|
import { describeError } from "./errors.js";
|
|
|
|
// Run a command and exit with its code once stdout/stderr have drained.
|
|
// Exiting before the drain can truncate piped output, and the library can keep
|
|
// the event loop alive after a command returns, so a plain return could hang.
|
|
// An error the command throws is printed as one `quak: MESSAGE` line, without
|
|
// the stack trace, and exits 1. MESSAGE is the error as `describeError` gives
|
|
// it, so it carries the reason Node's `fetch` puts on `cause`. A 401 from the
|
|
// server means it no longer accepts the saved session: that prints one line
|
|
// saying to log in again and exits 3, as a missing or corrupt session file
|
|
// does.
|
|
export const run = async (
|
|
command: Promise<number>,
|
|
stdout: Writable,
|
|
stderr: Writable,
|
|
exit: (code: number) => void,
|
|
): Promise<void> => {
|
|
let code: number;
|
|
try {
|
|
code = await command;
|
|
} catch (err) {
|
|
if (err instanceof ApiError && err.status === 401) {
|
|
stderr.write(
|
|
`quak: the saved session is no longer valid; run "quak login"\n`,
|
|
);
|
|
code = 3;
|
|
} else {
|
|
stderr.write(`quak: ${describeError(err)}\n`);
|
|
code = 1;
|
|
}
|
|
}
|
|
const pending = [stdout, stderr].filter((s) => s.writableLength > 0);
|
|
if (pending.length === 0) {
|
|
exit(code);
|
|
return;
|
|
}
|
|
let remaining = pending.length;
|
|
for (const s of pending) {
|
|
s.once("drain", () => {
|
|
if (--remaining === 0) exit(code);
|
|
});
|
|
}
|
|
};
|