- MCP
- blame on this site's /mcp
- CLI
- abso blame client/src/api-error.ts --json
- JSON
- /christopher/abso-core/blame/client/src/api-error.ts?format=json
62 linesFirst import 77% · pinch 23%Last change pinch in version 49,
62 lines from 2 changes in 2 versions.
init0Start the project1// How a call to the server fails: an ApiError, and whether the request ever went out.
3export class ApiError extends Error {
4 readonly status: number | undefined;
5 /** The connection never opened, so the request never reached the server (safe to run again). */
6 readonly notSent: boolean;
7 constructor(message: string, status?: number, notSent = false) {
8 super(message);
9 this.name = "ApiError";
10 this.status = status;
11 this.notSent = notSent;
12 }
13}
15/** Codes for a connection that never opened: the request never went out. */
16const NOT_SENT = new Set([
17 "UND_ERR_CONNECT_TIMEOUT",
18 "ECONNREFUSED",
19 "ENETUNREACH",
20 "EHOSTUNREACH",
21 "ENOTFOUND",
22 "EAI_AGAIN",
23 "EADDRNOTAVAIL",
24]);
26/** Whether a fetch failed before its request went out (connect timeout, refused, no route, no DNS answer). */
27export function neverSent(err: unknown): boolean {
28 const cause = (err as { cause?: { code?: string; errors?: readonly { code?: string }[] } }).cause;
29 if (cause === undefined) return false;
30 if (NOT_SENT.has(cause.code ?? "")) return true;
31 // Trying each address in turn (Node's autoSelectFamily) fails with every attempt's error.
32 return (
33 Array.isArray(cause.errors) &&
34 cause.errors.length > 0 &&
35 cause.errors.every((e) => NOT_SENT.has(e.code ?? "") || e.code === "ETIMEDOUT")
36 );
37}
pinch49Errors: say an HTML error page by its title and Ray ID
When the server fails before it can answer, its host sends an HTML error page instead. The client printed the page's first 300 characters, which are all doctype and IE comments, so nobody could tell what had failed: "GET /a/me/status answered 500: <!DOCTYPE html> <!--[if lt IE 7]> ...".
Now an HTML answer reads: answered 500: an HTML error page titled "Worker threw exception | ... | Cloudflare" (Ray ID ...), not the server's own answer: the server failed before it could say why. The title and Ray ID are what an operator needs to find the failure. JSON answers and plain text are said as before. Tested in error-sentence.test.ts.Compare39/**
40 * The server's `{error: "sentence"}` answer, or the start of whatever it sent. An HTML page (the host's error
41 * page, when the server itself failed) is said by its title and Ray ID, which its first lines never reach.
42 */
44 try {
45 const parsed = JSON.parse(body) as { error?: unknown; reason?: unknown };
46 if (typeof parsed.error === "string") return parsed.error;
47 if (typeof parsed.reason === "string") return parsed.reason;
48 } catch {
49 // Not JSON.
pinch49Errors: say an HTML error page by its title and Ray ID
When the server fails before it can answer, its host sends an HTML error page instead. The client printed the page's first 300 characters, which are all doctype and IE comments, so nobody could tell what had failed: "GET /a/me/status answered 500: <!DOCTYPE html> <!--[if lt IE 7]> ...".
Now an HTML answer reads: answered 500: an HTML error page titled "Worker threw exception | ... | Cloudflare" (Ray ID ...), not the server's own answer: the server failed before it could say why. The title and Ray ID are what an operator needs to find the failure. JSON answers and plain text are said as before. Tested in error-sentence.test.ts.Compare50 }
51 if (/^\s*<(!doctype html|html)/i.test(body)) {
52 const plain = (s: string) =>
53 s
54 .replace(/<[^>]*>/g, " ")
55 .replace(/\s+/g, " ")
56 .trim();
57 const title = plain(/<title>([\s\S]*?)<\/title>/i.exec(body)?.[1] ?? "");
58 const ray = /Ray ID:\s*(?:<[^>]*>\s*)*([0-9a-f]+)/i.exec(body)?.[1];
59 return `an HTML error page${title !== "" ? ` titled ${JSON.stringify(title)}` : ""}${ray ? ` (Ray ID ${ray})` : ""}, not the server's own answer: the server failed before it could say why`;
61 return body.slice(0, 300);
62}