🌐 AI搜索 & 代理 主页
Skip to content

Commit b33b9af

Browse files
author
Nik Samokhvalov
committed
fix(cli): improve init error output
- Avoid blank init failure messages; show code/detail/hint when available - Add regression test ensuring missing-connection error includes examples
1 parent 4a6066b commit b33b9af

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

cli/bin/postgres-ai.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,29 @@ program
255255
}
256256
} catch (error) {
257257
const errAny = error as any;
258-
const message = error instanceof Error ? error.message : String(error);
258+
let message = "";
259+
if (error instanceof Error && error.message) {
260+
message = error.message;
261+
} else if (errAny && typeof errAny === "object" && typeof errAny.message === "string" && errAny.message) {
262+
message = errAny.message;
263+
} else {
264+
message = String(error);
265+
}
266+
if (!message || message === "[object Object]") {
267+
message = "Unknown error";
268+
}
259269
console.error(`✗ init failed: ${message}`);
270+
if (errAny && typeof errAny === "object") {
271+
if (typeof errAny.code === "string" && errAny.code) {
272+
console.error(`Error code: ${errAny.code}`);
273+
}
274+
if (typeof errAny.detail === "string" && errAny.detail) {
275+
console.error(`Detail: ${errAny.detail}`);
276+
}
277+
if (typeof errAny.hint === "string" && errAny.hint) {
278+
console.error(`Hint: ${errAny.hint}`);
279+
}
280+
}
260281
if (errAny && typeof errAny === "object" && typeof errAny.code === "string") {
261282
if (errAny.code === "42501") {
262283
console.error("Hint: connect as a superuser (or a role with CREATEROLE and sufficient GRANT privileges).");

cli/test/init.test.cjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ test("resolveAdminConnection rejects when only PGPASSWORD is provided (no connec
9595
assert.throws(() => init.resolveAdminConnection({ envPassword: "pw" }), /Connection is required/);
9696
});
9797

98+
test("resolveAdminConnection error message includes examples", () => {
99+
assert.throws(() => init.resolveAdminConnection({}), /Examples:/);
100+
});
101+
98102
test("print-sql redaction regex matches password literal with embedded quotes", async () => {
99103
const plan = await init.buildInitPlan({
100104
database: "mydb",

0 commit comments

Comments
 (0)