forked from vercel/ai
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathretry-error.ts
66 lines (56 loc) · 1.52 KB
/
retry-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
import { AISDKError } from '@ai-sdk/provider';
const name = 'AI_RetryError';
const marker = `vercel.ai.error.${name}`;
const symbol = Symbol.for(marker);
export type RetryErrorReason =
| 'maxRetriesExceeded'
| 'errorNotRetryable'
| 'abort';
export class RetryError extends AISDKError {
private readonly [symbol] = true; // used in isInstance
// note: property order determines debugging output
readonly reason: RetryErrorReason;
readonly lastError: unknown;
readonly errors: Array<unknown>;
constructor({
message,
reason,
errors,
}: {
message: string;
reason: RetryErrorReason;
errors: Array<unknown>;
}) {
super({ name, message });
this.reason = reason;
this.errors = errors;
// separate our last error to make debugging via log easier:
this.lastError = errors[errors.length - 1];
}
static isInstance(error: unknown): error is RetryError {
return AISDKError.hasMarker(error, marker);
}
/**
* @deprecated use `isInstance` instead
*/
static isRetryError(error: unknown): error is RetryError {
return (
error instanceof Error &&
error.name === name &&
typeof (error as RetryError).reason === 'string' &&
Array.isArray((error as RetryError).errors)
);
}
/**
* @deprecated Do not use this method. It will be removed in the next major version.
*/
toJSON() {
return {
name: this.name,
message: this.message,
reason: this.reason,
lastError: this.lastError,
errors: this.errors,
};
}
}