Skip to content

Commit 94ef9d7

Browse files
RobertCraigiestainless-app[bot]
authored andcommitted
chore(types): nicer error class types + jsdocs (#1219)
1 parent 6e8c1d0 commit 94ef9d7

File tree

1 file changed

+24
-40
lines changed

1 file changed

+24
-40
lines changed

src/error.ts

+24-40
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,32 @@ import { castToError, Headers } from './core';
44

55
export class OpenAIError extends Error {}
66

7-
export class APIError extends OpenAIError {
8-
readonly status: number | undefined;
9-
readonly headers: Headers | undefined;
10-
readonly error: Object | undefined;
7+
export class APIError<
8+
TStatus extends number | undefined = number | undefined,
9+
THeaders extends Headers | undefined = Headers | undefined,
10+
TError extends Object | undefined = Object | undefined,
11+
> extends OpenAIError {
12+
/** HTTP status for the response that caused the error */
13+
readonly status: TStatus;
14+
/** HTTP headers for the response that caused the error */
15+
readonly headers: THeaders;
16+
/** JSON body of the response that caused the error */
17+
readonly error: TError;
1118

1219
readonly code: string | null | undefined;
1320
readonly param: string | null | undefined;
1421
readonly type: string | undefined;
1522

1623
readonly request_id: string | null | undefined;
1724

18-
constructor(
19-
status: number | undefined,
20-
error: Object | undefined,
21-
message: string | undefined,
22-
headers: Headers | undefined,
23-
) {
25+
constructor(status: TStatus, error: TError, message: string | undefined, headers: THeaders) {
2426
super(`${APIError.makeMessage(status, error, message)}`);
2527
this.status = status;
2628
this.headers = headers;
2729
this.request_id = headers?.['x-request-id'];
30+
this.error = error;
2831

2932
const data = error as Record<string, any>;
30-
this.error = data;
3133
this.code = data?.['code'];
3234
this.param = data?.['param'];
3335
this.type = data?.['type'];
@@ -60,7 +62,7 @@ export class APIError extends OpenAIError {
6062
message: string | undefined,
6163
headers: Headers | undefined,
6264
): APIError {
63-
if (!status) {
65+
if (!status || !headers) {
6466
return new APIConnectionError({ message, cause: castToError(errorResponse) });
6567
}
6668

@@ -102,17 +104,13 @@ export class APIError extends OpenAIError {
102104
}
103105
}
104106

105-
export class APIUserAbortError extends APIError {
106-
override readonly status: undefined = undefined;
107-
107+
export class APIUserAbortError extends APIError<undefined, undefined, undefined> {
108108
constructor({ message }: { message?: string } = {}) {
109109
super(undefined, undefined, message || 'Request was aborted.', undefined);
110110
}
111111
}
112112

113-
export class APIConnectionError extends APIError {
114-
override readonly status: undefined = undefined;
115-
113+
export class APIConnectionError extends APIError<undefined, undefined, undefined> {
116114
constructor({ message, cause }: { message?: string | undefined; cause?: Error | undefined }) {
117115
super(undefined, undefined, message || 'Connection error.', undefined);
118116
// in some environments the 'cause' property is already declared
@@ -127,35 +125,21 @@ export class APIConnectionTimeoutError extends APIConnectionError {
127125
}
128126
}
129127

130-
export class BadRequestError extends APIError {
131-
override readonly status: 400 = 400;
132-
}
128+
export class BadRequestError extends APIError<400, Headers> {}
133129

134-
export class AuthenticationError extends APIError {
135-
override readonly status: 401 = 401;
136-
}
130+
export class AuthenticationError extends APIError<401, Headers> {}
137131

138-
export class PermissionDeniedError extends APIError {
139-
override readonly status: 403 = 403;
140-
}
132+
export class PermissionDeniedError extends APIError<403, Headers> {}
141133

142-
export class NotFoundError extends APIError {
143-
override readonly status: 404 = 404;
144-
}
134+
export class NotFoundError extends APIError<404, Headers> {}
145135

146-
export class ConflictError extends APIError {
147-
override readonly status: 409 = 409;
148-
}
136+
export class ConflictError extends APIError<409, Headers> {}
149137

150-
export class UnprocessableEntityError extends APIError {
151-
override readonly status: 422 = 422;
152-
}
138+
export class UnprocessableEntityError extends APIError<422, Headers> {}
153139

154-
export class RateLimitError extends APIError {
155-
override readonly status: 429 = 429;
156-
}
140+
export class RateLimitError extends APIError<429, Headers> {}
157141

158-
export class InternalServerError extends APIError {}
142+
export class InternalServerError extends APIError<number, Headers> {}
159143

160144
export class LengthFinishReasonError extends OpenAIError {
161145
constructor() {

0 commit comments

Comments
 (0)