Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/client/src/api-error.ts?version=277&format=json
62 linesFirst import 77% · pinch 23%Last change pinch in version 49, 2 versions changed it

You're reading version 277. The newest is 318, and no version since then changed this file. Go to the newestCompare with 318

1// How a call to the server fails: an ApiError, and whether the request ever went out.
2
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 }
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 );
39/**
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 */
43export function errorSentence(body: string): string {
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.
50 }
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`;
60 }
61 return body.slice(0, 300);