@codefionn/llmleaf-client 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/error.js ADDED
@@ -0,0 +1,59 @@
1
+ // Typed error surface. Any non-2xx response carries the envelope
2
+ // {"error":{"message":"...", "type"?:"...", "code"?:"..."}}
3
+ // (SPEC.md "Errors"). We parse it into ApiError and throw.
4
+ /**
5
+ * Thrown for any non-2xx HTTP response from the gateway.
6
+ *
7
+ * Status codes (SPEC.md): 400 bad request · 401 missing/invalid key ·
8
+ * 403 blocked or model-not-allowed · 404 no route for model ·
9
+ * 429 key suspended (limiter) · 502 all upstreams failed.
10
+ */
11
+ export class ApiError extends Error {
12
+ status;
13
+ /** present on some dialects; absent on the llmleaf core envelope. */
14
+ type;
15
+ code;
16
+ constructor(status, message, opts) {
17
+ super(message);
18
+ this.name = "ApiError";
19
+ this.status = status;
20
+ this.type = opts?.type;
21
+ this.code = opts?.code;
22
+ // Restore prototype chain for instanceof across transpile targets.
23
+ Object.setPrototypeOf(this, ApiError.prototype);
24
+ }
25
+ }
26
+ /**
27
+ * Build an {@link ApiError} from a non-2xx response. Reads the body once. Falls back
28
+ * to the HTTP status text when the body is missing or not the expected envelope.
29
+ */
30
+ export async function apiErrorFromResponse(res) {
31
+ const fallback = res.statusText || `HTTP ${res.status}`;
32
+ let text;
33
+ try {
34
+ text = await res.text();
35
+ }
36
+ catch {
37
+ return new ApiError(res.status, fallback);
38
+ }
39
+ if (!text)
40
+ return new ApiError(res.status, fallback);
41
+ try {
42
+ const parsed = JSON.parse(text);
43
+ const body = parsed && typeof parsed === "object" && "error" in parsed
44
+ ? parsed.error
45
+ : parsed;
46
+ if (body && typeof body === "object") {
47
+ const message = typeof body.message === "string" && body.message ? body.message : fallback;
48
+ return new ApiError(res.status, message, {
49
+ type: typeof body.type === "string" ? body.type : undefined,
50
+ code: typeof body.code === "string" ? body.code : undefined,
51
+ });
52
+ }
53
+ }
54
+ catch {
55
+ // Not JSON — fall through and surface the raw text.
56
+ }
57
+ return new ApiError(res.status, text.slice(0, 2048));
58
+ }
59
+ //# sourceMappingURL=error.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error.js","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":"AAAA,iEAAiE;AACjE,8DAA8D;AAC9D,2DAA2D;AAE3D;;;;;;GAMG;AACH,MAAM,OAAO,QAAS,SAAQ,KAAK;IACxB,MAAM,CAAS;IACxB,qEAAqE;IAC5D,IAAI,CAAU;IACd,IAAI,CAAU;IAEvB,YACE,MAAc,EACd,OAAe,EACf,IAAuC;QAEvC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC;QACvB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,IAAI,EAAE,IAAI,CAAC;QACvB,IAAI,CAAC,IAAI,GAAG,IAAI,EAAE,IAAI,CAAC;QACvB,mEAAmE;QACnE,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,QAAQ,CAAC,SAAS,CAAC,CAAC;IAClD,CAAC;CACF;AAQD;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CAAC,GAAa;IACtD,MAAM,QAAQ,GAAG,GAAG,CAAC,UAAU,IAAI,QAAQ,GAAG,CAAC,MAAM,EAAE,CAAC;IACxD,IAAI,IAAY,CAAC;IACjB,IAAI,CAAC;QACH,IAAI,GAAG,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC;IAC1B,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,QAAQ,CAAC,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IAC5C,CAAC;IACD,IAAI,CAAC,IAAI;QAAE,OAAO,IAAI,QAAQ,CAAC,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IAErD,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,IAAI,CAA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