Skip to content

Commit 5a07ea2

Browse files
fix(cli): catalog says the registry could not be read instead of listing nothing (heygen-com#7015)
1 parent 346f0e4 commit 5a07ea2

4 files changed

Lines changed: 55 additions & 12 deletions

File tree

‎packages/cli/src/commands/catalog.test.ts‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ describe("countUnindexed", () => {
7373

7474
const state = vi.hoisted(() => ({
7575
registry: [] as Array<{ name: string; type: string; tags?: string[] }>,
76+
registryUnreadable: "",
7677
artifactRevision: "revision-current",
7778
cachedVectorRevision: "revision-current",
7879
vectorFetches: 0,
@@ -108,10 +109,10 @@ vi.mock("../registry/resolver.js", () => ({
108109
}));
109110

110111
vi.mock("../registry/remote.js", () => ({
111-
fetchRegistryManifest: async () => ({
112-
items: state.registry,
113-
catalogArtifact: { revision: state.artifactRevision },
114-
}),
112+
readRegistryManifest: async () => {
113+
if (state.registryUnreadable) throw new Error(state.registryUnreadable);
114+
return { items: state.registry, catalogArtifact: { revision: state.artifactRevision } };
115+
},
115116
}));
116117

117118
// The terminal and CI switches below decide attendance; the agent running the suite must not.
@@ -276,6 +277,7 @@ async function runForExit(
276277
}
277278

278279
beforeEach(() => {
280+
state.registryUnreadable = "";
279281
state.modelStatus = "ready";
280282
state.artifactRevision = "revision-current";
281283
state.cachedVectorRevision = "revision-current";
@@ -301,6 +303,20 @@ beforeEach(() => {
301303
];
302304
});
303305

306+
describe("catalog when the registry cannot be read", () => {
307+
it("exits non-zero with the reason instead of printing an empty list", async () => {
308+
state.registryUnreadable =
309+
"The registry could not be read: fetch failed (getaddrinfo ENOTFOUND raw.githubusercontent.com)";
310+
311+
const { exitCode, err } = await runForExit({ json: true });
312+
313+
expect(exitCode).toBe(1);
314+
expect(err).toContain("The registry could not be read: fetch failed");
315+
expect(err).toContain("raw.githubusercontent.com");
316+
expect(err).not.toContain("[]");
317+
});
318+
});
319+
304320
describe("catalog --json meaning search", () => {
305321
it("reports the registry moves meaning search cannot see", async () => {
306322
// Published after the user's artifact was fetched: in the registry, absent

‎packages/cli/src/commands/catalog.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import * as clack from "../ui/prompts.js";
1414
import { realpath, type ItemType, type RegistryItem } from "@hyperframes/core";
1515
import { c } from "../ui/colors.js";
1616
import { loadAllItems } from "../registry/resolver.js";
17-
import { fetchRegistryManifest } from "../registry/remote.js";
17+
import { readRegistryManifest } from "../registry/remote.js";
1818
import { loadProjectConfig, DEFAULT_PROJECT_CONFIG } from "../utils/projectConfig.js";
1919
import { relative, resolve } from "node:path";
2020
import { finishCommand } from "../utils/commandResult.js";
@@ -232,9 +232,12 @@ export default defineCommand({
232232

233233
// Asked for the whole manifest on purpose: its item list defines coverage,
234234
// and its artifact revision is the one owner of vector freshness.
235-
const manifest = await fetchRegistryManifest(config.registry);
236-
const entries = manifest?.items ?? [];
237-
const artifactRevision = manifest?.catalogArtifact?.revision;
235+
const manifest = await readRegistryManifest(config.registry).catch((error: unknown) => {
236+
console.error(error instanceof Error ? error.message : String(error));
237+
return finishCommand(1);
238+
});
239+
const entries = manifest.items;
240+
const artifactRevision = manifest.catalogArtifact?.revision;
238241
const catalog = entries.filter((e) => e.type !== "hyperframes:example");
239242
const registryNames = new Set(catalog.map((e) => e.name));
240243
const filtered = typeFilter ? catalog.filter((e) => e.type === typeFilter) : catalog;

‎packages/cli/src/registry/remote.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const {
1818
fetchItemFile,
1919
fetchItemManifest,
2020
fetchRegistryManifest,
21+
readRegistryManifest,
2122
DEFAULT_REGISTRY_URL,
2223
} = await import("./remote.js");
2324

@@ -76,6 +77,21 @@ afterAll(() => {
7677
rmSync(scratchHome, { recursive: true, force: true });
7778
});
7879

80+
describe("readRegistryManifest", () => {
81+
it("throws why when the registry host cannot be reached and nothing is cached", async () => {
82+
vi.spyOn(globalThis, "fetch").mockRejectedValue(
83+
Object.assign(new TypeError("fetch failed"), {
84+
cause: Object.assign(new Error("getaddrinfo ENOTFOUND raw.githubusercontent.com"), {
85+
code: "ENOTFOUND",
86+
}),
87+
}),
88+
);
89+
await expect(readRegistryManifest(DEFAULT_REGISTRY_URL)).rejects.toThrow(
90+
"The registry could not be read: fetch failed (getaddrinfo ENOTFOUND raw.githubusercontent.com)",
91+
);
92+
});
93+
});
94+
7995
describe("fetchRegistryManifest", () => {
8096
it("rejects poisoned stale cache data instead of returning it on network failure", async () => {
8197
vi.spyOn(globalThis, "fetch").mockResolvedValueOnce(ok(MANIFEST));

‎packages/cli/src/registry/remote.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,17 @@ async function fetchJson<T>(url: string, validate: (value: unknown) => value is
123123
* because "check for something newer" and "rather have nothing than this" are
124124
* different requests and only the first one is ever made.
125125
*/
126-
export async function fetchRegistryManifest(
126+
export const fetchRegistryManifest = (
127127
baseUrl: string = DEFAULT_REGISTRY_URL,
128128
options?: { skipCache?: boolean },
129-
): Promise<RegistryManifest | undefined> {
129+
): Promise<RegistryManifest | undefined> =>
130+
readRegistryManifest(baseUrl, options).catch(() => undefined);
131+
132+
/** The manifest as fetchRegistryManifest gives it, but unreachable and never cached throws why, never undefined. */
133+
export async function readRegistryManifest(
134+
baseUrl: string = DEFAULT_REGISTRY_URL,
135+
options?: { skipCache?: boolean },
136+
): Promise<RegistryManifest> {
130137
const url = registryPathUrl(baseUrl, "registry.json");
131138
const cacheFile = cachePath(baseUrl, "registry");
132139
const cached = readCacheEntry(cacheFile, validRegistryManifest);
@@ -136,8 +143,9 @@ export async function fetchRegistryManifest(
136143
const manifest = await fetchJson(url, validRegistryManifest);
137144
writeCache(cacheFile, manifest);
138145
return manifest;
139-
} catch {
140-
return cached?.data;
146+
} catch (err) {
147+
if (cached) return cached.data;
148+
throw new Error(`The registry could not be read: ${describeCauseChain(err)}`, { cause: err });
141149
}
142150
}
143151

0 commit comments

Comments
 (0)