forked from zaber-dev/free-ai-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.ts
More file actions
59 lines (52 loc) · 1.57 KB
/
Copy patherrors.ts
File metadata and controls
59 lines (52 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { Capability } from "../capabilities/capabilities";
export class ProviderError extends Error {
public status: number;
public retryAfterMs?: number;
public rawBody?: string;
public url?: string;
constructor(message: string, status: number, retryAfterMs?: number, rawBody?: string, url?: string) {
super(message);
this.name = "ProviderError";
this.status = status;
this.retryAfterMs = retryAfterMs;
this.rawBody = rawBody;
this.url = url;
}
static async fromResponse(url: string, response: Response): Promise<ProviderError> {
let body = "";
try {
body = await response.text();
} catch {
body = "<unreadable body>";
}
const retryAfterHeader = response.headers.get("retry-after");
let retryAfterMs: number | undefined;
if (retryAfterHeader) {
const parsed = parseInt(retryAfterHeader, 10);
if (!isNaN(parsed)) {
retryAfterMs = parsed * 1000;
}
}
return new ProviderError(
`Provider request failed with status ${response.status}: ${body}`,
response.status,
retryAfterMs,
body,
url
);
}
}
export class NoProviderAvailableError extends Error {
public capabilities: Capability[];
public attempted: string[];
constructor(capabilities: Capability[], attempted: string[]) {
super(
`No available provider could fulfill capabilities: [${capabilities.join(
", "
)}]. Attempted: [${attempted.join(", ")}]`
);
this.name = "NoProviderAvailableError";
this.capabilities = capabilities;
this.attempted = attempted;
}
}