@@ -4,30 +4,32 @@ import { castToError, Headers } from './core';
4
4
5
5
export class OpenAIError extends Error { }
6
6
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 ;
11
18
12
19
readonly code : string | null | undefined ;
13
20
readonly param : string | null | undefined ;
14
21
readonly type : string | undefined ;
15
22
16
23
readonly request_id : string | null | undefined ;
17
24
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 ) {
24
26
super ( `${ APIError . makeMessage ( status , error , message ) } ` ) ;
25
27
this . status = status ;
26
28
this . headers = headers ;
27
29
this . request_id = headers ?. [ 'x-request-id' ] ;
30
+ this . error = error ;
28
31
29
32
const data = error as Record < string , any > ;
30
- this . error = data ;
31
33
this . code = data ?. [ 'code' ] ;
32
34
this . param = data ?. [ 'param' ] ;
33
35
this . type = data ?. [ 'type' ] ;
@@ -60,7 +62,7 @@ export class APIError extends OpenAIError {
60
62
message : string | undefined ,
61
63
headers : Headers | undefined ,
62
64
) : APIError {
63
- if ( ! status ) {
65
+ if ( ! status || ! headers ) {
64
66
return new APIConnectionError ( { message, cause : castToError ( errorResponse ) } ) ;
65
67
}
66
68
@@ -102,17 +104,13 @@ export class APIError extends OpenAIError {
102
104
}
103
105
}
104
106
105
- export class APIUserAbortError extends APIError {
106
- override readonly status : undefined = undefined ;
107
-
107
+ export class APIUserAbortError extends APIError < undefined , undefined , undefined > {
108
108
constructor ( { message } : { message ?: string } = { } ) {
109
109
super ( undefined , undefined , message || 'Request was aborted.' , undefined ) ;
110
110
}
111
111
}
112
112
113
- export class APIConnectionError extends APIError {
114
- override readonly status : undefined = undefined ;
115
-
113
+ export class APIConnectionError extends APIError < undefined , undefined , undefined > {
116
114
constructor ( { message, cause } : { message ?: string | undefined ; cause ?: Error | undefined } ) {
117
115
super ( undefined , undefined , message || 'Connection error.' , undefined ) ;
118
116
// in some environments the 'cause' property is already declared
@@ -127,35 +125,21 @@ export class APIConnectionTimeoutError extends APIConnectionError {
127
125
}
128
126
}
129
127
130
- export class BadRequestError extends APIError {
131
- override readonly status : 400 = 400 ;
132
- }
128
+ export class BadRequestError extends APIError < 400 , Headers > { }
133
129
134
- export class AuthenticationError extends APIError {
135
- override readonly status : 401 = 401 ;
136
- }
130
+ export class AuthenticationError extends APIError < 401 , Headers > { }
137
131
138
- export class PermissionDeniedError extends APIError {
139
- override readonly status : 403 = 403 ;
140
- }
132
+ export class PermissionDeniedError extends APIError < 403 , Headers > { }
141
133
142
- export class NotFoundError extends APIError {
143
- override readonly status : 404 = 404 ;
144
- }
134
+ export class NotFoundError extends APIError < 404 , Headers > { }
145
135
146
- export class ConflictError extends APIError {
147
- override readonly status : 409 = 409 ;
148
- }
136
+ export class ConflictError extends APIError < 409 , Headers > { }
149
137
150
- export class UnprocessableEntityError extends APIError {
151
- override readonly status : 422 = 422 ;
152
- }
138
+ export class UnprocessableEntityError extends APIError < 422 , Headers > { }
153
139
154
- export class RateLimitError extends APIError {
155
- override readonly status : 429 = 429 ;
156
- }
140
+ export class RateLimitError extends APIError < 429 , Headers > { }
157
141
158
- export class InternalServerError extends APIError { }
142
+ export class InternalServerError extends APIError < number , Headers > { }
159
143
160
144
export class LengthFinishReasonError extends OpenAIError {
161
145
constructor ( ) {
0 commit comments