Skip to content

Commit 7469b54

Browse files
authored
fix(cli): render plugin search JSON failures (#127679)
1 parent 1d5d5cf commit 7469b54

3 files changed

Lines changed: 94 additions & 2 deletions

File tree

‎src/cli/plugins-search-command.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,26 @@ describe("plugins search command", () => {
111111
expect(mocks.runtime.writeJson).toHaveBeenCalledWith({ results: [] }, 2);
112112
});
113113

114+
it("leaves missing-query JSON failures to the root renderer", async () => {
115+
await expect(runPluginsSearchCommand([], { json: true }, mocks.runtime)).rejects.toThrow(
116+
"Usage: openclaw plugins search <query>",
117+
);
118+
119+
expect(mocks.runtime.error).not.toHaveBeenCalled();
120+
expect(mocks.runtime.exit).not.toHaveBeenCalled();
121+
});
122+
123+
it("leaves ClawHub JSON failures to the root renderer", async () => {
124+
mocks.searchClawHubPackages.mockRejectedValueOnce(new Error("offline fixture"));
125+
126+
await expect(
127+
runPluginsSearchCommand("calendar", { json: true }, mocks.runtime),
128+
).rejects.toThrow("offline fixture");
129+
130+
expect(mocks.runtime.error).not.toHaveBeenCalled();
131+
expect(mocks.runtime.exit).not.toHaveBeenCalled();
132+
});
133+
114134
it("rejects partial numeric search limits", async () => {
115135
const program = new Command();
116136
program.exitOverride();

‎src/cli/plugins-search-command.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { ClawHubPackageSearchResult } from "../infra/clawhub-packages.js";
55
import { formatErrorMessage } from "../infra/errors.js";
66
import { searchInstallablePluginPackages } from "../plugins/catalog-search.js";
77
import { defaultRuntime, writeRuntimeJson, type RuntimeEnv } from "../runtime.js";
8+
import { ExpectedCliError } from "./failure-output.js";
89

910
/** Options accepted by `openclaw plugins search`. */
1011
type PluginsSearchOptions = {
@@ -34,8 +35,8 @@ export async function runPluginsSearchCommand(
3435
Array.isArray(queryParts) ? queryParts.join(" ") : queryParts,
3536
);
3637
if (!query) {
37-
runtime.error("Usage: openclaw plugins search <query>");
38-
return runtime.exit(1);
38+
const message = "Usage: openclaw plugins search <query>";
39+
throw new ExpectedCliError({ message, humanOutput: message, machineOutput: message });
3940
}
4041

4142
try {
@@ -52,6 +53,9 @@ export async function runPluginsSearchCommand(
5253
runtime.log(`${theme.heading("ClawHub plugins")} ${theme.muted(`(${results.length})`)}`);
5354
runtime.log(results.map(formatPackageSearchLine).join("\n"));
5455
} catch (error) {
56+
if (opts.json) {
57+
throw error;
58+
}
5559
runtime.error(formatErrorMessage(error));
5660
runtime.exit(1);
5761
}

‎test/cli-json-stdout.e2e.test.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,74 @@ describe("cli json stdout contract", () => {
474474
);
475475
});
476476

477+
it.each([
478+
{
479+
name: "the search query is missing",
480+
args: ["plugins", "search", "--json"],
481+
message: "Usage: openclaw plugins search <query>",
482+
},
483+
{
484+
name: "ClawHub transport fails",
485+
args: ["plugins", "search", "fixture", "--json"],
486+
message: "offline fixture",
487+
},
488+
])("returns one canonical JSON document when plugins $name", async (testCase) => {
489+
await withTempHome(
490+
async (tempHome) => {
491+
const preload = `data:text/javascript,${encodeURIComponent(
492+
'globalThis.fetch = async () => { throw new Error("offline fixture"); };',
493+
)}`;
494+
const result = runBuiltCli(tempHome, testCase.args, {
495+
NODE_OPTIONS: `--import=${preload}`,
496+
OPENCLAW_STATE_DIR: path.join(tempHome, "isolated-state"),
497+
OPENCLAW_CONFIG_PATH: path.join(tempHome, "missing-openclaw.json"),
498+
CLAWHUB_CONFIG_PATH: path.join(tempHome, "missing-clawhub.json"),
499+
CLAWHUB_TOKEN: "",
500+
CLAWHUB_AUTH_TOKEN: "",
501+
});
502+
503+
expect(result.status, result.stderr).toBe(1);
504+
expect(result.stdout, result.stderr).not.toBe("");
505+
expect(result.stdout).not.toMatch(/[\u001B\u0007]/u);
506+
expect(JSON.parse(result.stdout)).toEqual({
507+
ok: false,
508+
error: {
509+
type: "cli_error",
510+
message: testCase.message,
511+
},
512+
});
513+
expect(result.stderr).toContain(testCase.message);
514+
},
515+
{ prefix: "openclaw-plugins-json-failure-e2e-" },
516+
);
517+
});
518+
519+
it("keeps plugins search JSON failures clean through dual-TTY finalization", async () => {
520+
await withTempHome(
521+
async (tempHome) => {
522+
const preload = `data:text/javascript,${encodeURIComponent(
523+
'Object.defineProperty(process.stdout, "isTTY", { value: true, configurable: true }); Object.defineProperty(process.stderr, "isTTY", { value: true, configurable: true });',
524+
)}`;
525+
const result = runBuiltCli(tempHome, ["plugins", "search", "--json"], {
526+
NODE_OPTIONS: `--import=${preload}`,
527+
});
528+
529+
expect(result.status, result.stderr).toBe(1);
530+
expect(JSON.parse(result.stdout)).toEqual({
531+
ok: false,
532+
error: {
533+
type: "cli_error",
534+
message: "Usage: openclaw plugins search <query>",
535+
},
536+
});
537+
expect(result.stdout).not.toMatch(/[\u001B\u0007]/u);
538+
expect(result.stderr).toContain("Usage: openclaw plugins search <query>");
539+
expect(result.stderr).toContain("\u001B[?25h");
540+
},
541+
{ prefix: "openclaw-plugins-json-tty-failure-e2e-" },
542+
);
543+
});
544+
477545
it.each([
478546
{
479547
name: "search with a leaf JSON flag",

0 commit comments

Comments
 (0)