Skip to content

Commit 90fbde9

Browse files
Copilotfuxingloh
andcommitted
refactor: clarify error format resolution
Co-authored-by: fuxingloh <4266087+fuxingloh@users.noreply.github.com>
1 parent 1fa97d2 commit 90fbde9

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

packages/use-agently/src/bin.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ import { handleCliError } from "./errors.js";
77

88
installTelemetry(cli);
99

10+
function resolveOutputFormat(): "tui" | "json" {
11+
const opts = cli.optsWithGlobals?.();
12+
if (opts?.output === "tui" || opts?.output === "json") return opts.output;
13+
return process.stderr.isTTY ? "tui" : "json";
14+
}
15+
1016
try {
1117
await cli.parseAsync();
1218
await Promise.all([checkAutoUpdate(), flushTelemetry()]);
1319
} catch (err) {
14-
const format = (cli.optsWithGlobals?.().output ?? (process.stderr.isTTY ? "tui" : "json")) as "tui" | "json";
15-
handleCliError(err, format);
20+
handleCliError(err, resolveOutputFormat());
1621
}

packages/use-agently/src/errors.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,23 @@ describe("handleCliError", () => {
2222

2323
test("prints json error when format is json", () => {
2424
expect(() => handleCliError(new Error("boom"), "json")).toThrow("exit:1");
25+
expect(exitSpy).toHaveBeenCalledWith(1);
2526
expect(errorSpy).toHaveBeenCalledWith(JSON.stringify({ error: { message: "boom" } }));
2627
});
2728

2829
test("prints plain text when format is tui and stderr is not a TTY", () => {
2930
Object.defineProperty(process.stderr, "isTTY", { value: false, configurable: true });
3031

3132
expect(() => handleCliError(new Error("oops"), "tui")).toThrow("exit:1");
33+
expect(exitSpy).toHaveBeenCalledWith(1);
3234
expect(errorSpy).toHaveBeenCalledWith("Error: oops");
3335
});
3436

3537
test("prints boxed error when format is tui and stderr is a TTY", () => {
3638
Object.defineProperty(process.stderr, "isTTY", { value: true, configurable: true });
3739

3840
expect(() => handleCliError(new Error("boxed"), "tui")).toThrow("exit:1");
41+
expect(exitSpy).toHaveBeenCalledWith(1);
3942
const output = errorSpy.mock.calls.map((c) => c[0]).join("");
4043
expect(output).toContain("boxed");
4144
expect(output).toContain("Error");

0 commit comments

Comments
 (0)