forked from vercel/ai
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdownload-error.ts
68 lines (60 loc) · 1.7 KB
/
download-error.ts
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
60
61
62
63
64
65
66
67
68
import { AISDKError } from '@ai-sdk/provider';
const name = 'AI_DownloadError';
const marker = `vercel.ai.error.${name}`;
const symbol = Symbol.for(marker);
export class DownloadError extends AISDKError {
private readonly [symbol] = true; // used in isInstance
readonly url: string;
readonly statusCode?: number;
readonly statusText?: string;
constructor({
url,
statusCode,
statusText,
cause,
message = cause == null
? `Failed to download ${url}: ${statusCode} ${statusText}`
: `Failed to download ${url}: ${cause}`,
}: {
url: string;
statusCode?: number;
statusText?: string;
message?: string;
cause?: unknown;
}) {
super({ name, message, cause });
this.url = url;
this.statusCode = statusCode;
this.statusText = statusText;
}
static isInstance(error: unknown): error is DownloadError {
return AISDKError.hasMarker(error, marker);
}
/**
* @deprecated use `isInstance` instead
*/
static isDownloadError(error: unknown): error is DownloadError {
return (
error instanceof Error &&
error.name === name &&
typeof (error as DownloadError).url === 'string' &&
((error as DownloadError).statusCode == null ||
typeof (error as DownloadError).statusCode === 'number') &&
((error as DownloadError).statusText == null ||
typeof (error as DownloadError).statusText === 'string')
);
}
/**
* @deprecated Do not use this method. It will be removed in the next major version.
*/
toJSON() {
return {
name: this.name,
message: this.message,
url: this.url,
statusCode: this.statusCode,
statusText: this.statusText,
cause: this.cause,
};
}
}