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:
Florian Schmidt
2026-11-16 10:31:11 +01:00
parent 83e4e3f15e
commit 324ad4b39a
9 changed files with 91 additions and 9 deletions
+17 -3
View File
@@ -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>);