-
Notifications
You must be signed in to change notification settings - Fork 19
/
error.ts
184 lines (149 loc) · 4.61 KB
/
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
import { castToError, Headers } from "./core.ts";
export class OpenAIError extends Error {}
export class APIError extends OpenAIError {
readonly status: number | undefined;
readonly headers: Headers | undefined;
readonly error: Object | undefined;
readonly code: string | null | undefined;
readonly param: string | null | undefined;
readonly type: string | undefined;
readonly request_id: string | null | undefined;
constructor(
status: number | undefined,
error: Object | undefined,
message: string | undefined,
headers: Headers | undefined,
) {
super(`${APIError.makeMessage(status, error, message)}`);
this.status = status;
this.headers = headers;
this.request_id = headers?.["x-request-id"];
const data = error as Record<string, any>;
this.error = data;
this.code = data?.["code"];
this.param = data?.["param"];
this.type = data?.["type"];
}
private static makeMessage(
status: number | undefined,
error: any,
message: string | undefined,
) {
const msg = error?.message
? typeof error.message === "string"
? error.message
: JSON.stringify(error.message)
: error
? JSON.stringify(error)
: message;
if (status && msg) {
return `${status} ${msg}`;
}
if (status) {
return `${status} status code (no body)`;
}
if (msg) {
return msg;
}
return "(no status code or body)";
}
static generate(
status: number | undefined,
errorResponse: Object | undefined,
message: string | undefined,
headers: Headers | undefined,
) {
if (!status) {
return new APIConnectionError({
message,
cause: castToError(errorResponse),
});
}
const error = (errorResponse as Record<string, any>)?.["error"];
if (status === 400) {
return new BadRequestError(status, error, message, headers);
}
if (status === 401) {
return new AuthenticationError(status, error, message, headers);
}
if (status === 403) {
return new PermissionDeniedError(status, error, message, headers);
}
if (status === 404) {
return new NotFoundError(status, error, message, headers);
}
if (status === 409) {
return new ConflictError(status, error, message, headers);
}
if (status === 422) {
return new UnprocessableEntityError(status, error, message, headers);
}
if (status === 429) {
return new RateLimitError(status, error, message, headers);
}
if (status >= 500) {
return new InternalServerError(status, error, message, headers);
}
return new APIError(status, error, message, headers);
}
}
export class APIUserAbortError extends APIError {
override readonly status: undefined = undefined;
constructor({ message }: { message?: string } = {}) {
super(undefined, undefined, message || "Request was aborted.", undefined);
}
}
export class APIConnectionError extends APIError {
override readonly status: undefined = undefined;
constructor(
{ message, cause }: {
message?: string | undefined;
cause?: Error | undefined;
},
) {
super(undefined, undefined, message || "Connection error.", undefined);
// in some environments the 'cause' property is already declared
// @ts-ignore
if (cause) this.cause = cause;
}
}
export class APIConnectionTimeoutError extends APIConnectionError {
constructor({ message }: { message?: string } = {}) {
super({ message: message ?? "Request timed out." });
}
}
export class BadRequestError extends APIError {
override readonly status: 400 = 400;
}
export class AuthenticationError extends APIError {
override readonly status: 401 = 401;
}
export class PermissionDeniedError extends APIError {
override readonly status: 403 = 403;
}
export class NotFoundError extends APIError {
override readonly status: 404 = 404;
}
export class ConflictError extends APIError {
override readonly status: 409 = 409;
}
export class UnprocessableEntityError extends APIError {
override readonly status: 422 = 422;
}
export class RateLimitError extends APIError {
override readonly status: 429 = 429;
}
export class InternalServerError extends APIError {}
export class LengthFinishReasonError extends OpenAIError {
constructor() {
super(`Could not parse response content as the length limit was reached`);
}
}
export class ContentFilterFinishReasonError extends OpenAIError {
constructor() {
super(
`Could not parse response content as the request was rejected by the content filter`,
);
}
}