fix: show why SSH connection tests and runs fail (#7)
Test connection, key installation and runs now tell authentication failures apart from unreachable hosts (refused, timeout, DNS, ...) instead of a generic "SSH connection failed". The web client keeps the server's deliberate error messages (4xx, 502, 503) and shows them in the hosts page; unexpected 500 errors still show only the status. The reload hint appears only on 409 conflicts.
This commit is contained in:
@@ -58,7 +58,12 @@ export function HostsPage({ onChange }: { onChange?: () => void }) {
|
||||
try {
|
||||
await work();
|
||||
} catch (e) {
|
||||
setError(`${String(e)}. Reload hosts after a conflict before retrying.`);
|
||||
const message = e instanceof Error ? e.message : String(e);
|
||||
setError(
|
||||
(e as { status?: number }).status === 409
|
||||
? `${message}. Reload hosts before retrying.`
|
||||
: message,
|
||||
);
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
|
||||
+17
-3
@@ -9,8 +9,21 @@ import type {
|
||||
} from "@shellbook/core";
|
||||
|
||||
export class HttpError extends Error {
|
||||
constructor(public status: number) {
|
||||
super(`Request failed (${status})`);
|
||||
constructor(
|
||||
public status: number,
|
||||
serverMessage?: string,
|
||||
) {
|
||||
super(serverMessage || `Request failed (${status})`);
|
||||
}
|
||||
}
|
||||
/** Deliberate errors (4xx, 502, 503) carry a user-facing `message`; unexpected 500s may expose internals. */
|
||||
async function serverMessage(response: Response): Promise<string | undefined> {
|
||||
if (response.status === 500) return undefined;
|
||||
try {
|
||||
const body = (await response.json()) as { message?: unknown };
|
||||
return typeof body.message === "string" ? body.message.slice(0, 500) : undefined;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
export async function json<T>(
|
||||
@@ -24,7 +37,8 @@ export async function json<T>(
|
||||
headers: body === undefined ? {} : { "Content-Type": "application/json" },
|
||||
body: body === undefined ? undefined : JSON.stringify(body),
|
||||
});
|
||||
if (!response.ok) throw new HttpError(response.status);
|
||||
if (!response.ok)
|
||||
throw new HttpError(response.status, await serverMessage(response));
|
||||
return response.status === 204
|
||||
? (undefined as T)
|
||||
: (response.json() as Promise<T>);
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
import { HttpError, json } from "../api";
|
||||
afterEach(() => vi.unstubAllGlobals());
|
||||
it("keeps the server's error message and status", async () => {
|
||||
vi.stubGlobal("fetch", vi.fn(async () => new Response(JSON.stringify({ statusCode: 502, error: "Bad Gateway", message: "SSH authentication failed: check the user and the saved password or key" }), { status: 502, headers: { "Content-Type": "application/json" } })));
|
||||
const error = (await json("/x").catch((e: HttpError) => e)) as HttpError;
|
||||
expect(error).toBeInstanceOf(HttpError);
|
||||
expect(error.status).toBe(502);
|
||||
expect(error.message).toBe("SSH authentication failed: check the user and the saved password or key");
|
||||
});
|
||||
it("falls back to the status when the body is not JSON", async () => {
|
||||
vi.stubGlobal("fetch", vi.fn(async () => new Response("<html>", { status: 500 })));
|
||||
expect(((await json("/x").catch((e: HttpError) => e)) as HttpError).message).toBe("Request failed (500)");
|
||||
});
|
||||
it("does not surface messages of unexpected server errors", async () => {
|
||||
vi.stubGlobal("fetch", vi.fn(async () => new Response(JSON.stringify({ statusCode: 500, message: "EACCES: permission denied, open '/var/lib/shellbook/x'" }), { status: 500 })));
|
||||
expect(((await json("/x").catch((e: HttpError) => e)) as HttpError).message).toBe("Request failed (500)");
|
||||
});
|
||||
@@ -117,3 +117,14 @@ it("loads external changes without silently changing the version of an open edit
|
||||
),
|
||||
);
|
||||
});
|
||||
it("shows the server's reason for a failed connection test without a conflict hint", async () => {
|
||||
vi.mocked(json).mockImplementation(async (url) => {
|
||||
if (url.endsWith("/action")) throw Object.assign(new Error("SSH authentication failed: check the user and the saved password or key"), { status: 502 });
|
||||
return inventory;
|
||||
});
|
||||
render(<HostsPage />);
|
||||
await screen.findByText("demo");
|
||||
fireEvent.click(screen.getByText("Test connection to demo"));
|
||||
const alert = await screen.findByText(/SSH authentication failed/);
|
||||
expect(alert.textContent).toBe("SSH authentication failed: check the user and the saved password or key");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user