@@ -4,17 +4,19 @@ import { castToError, Headers } from './core';
4
4
5
5
export class GroqError extends Error { }
6
6
7
- export class APIError extends GroqError {
8
- readonly status : number | undefined ;
9
- readonly headers : Headers | undefined ;
10
- readonly error : Object | undefined ;
11
-
12
- constructor (
13
- status : number | undefined ,
14
- error : Object | undefined ,
15
- message : string | undefined ,
16
- headers : Headers | undefined ,
17
- ) {
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 GroqError {
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 ;
18
+
19
+ constructor ( status : TStatus , error : TError , message : string | undefined , headers : THeaders ) {
18
20
super ( `${ APIError . makeMessage ( status , error , message ) } ` ) ;
19
21
this . status = status ;
20
22
this . headers = headers ;
@@ -48,7 +50,7 @@ export class APIError extends GroqError {
48
50
message : string | undefined ,
49
51
headers : Headers | undefined ,
50
52
) : APIError {
51
- if ( ! status ) {
53
+ if ( ! status || ! headers ) {
52
54
return new APIConnectionError ( { message, cause : castToError ( errorResponse ) } ) ;
53
55
}
54
56
@@ -90,17 +92,13 @@ export class APIError extends GroqError {
90
92
}
91
93
}
92
94
93
- export class APIUserAbortError extends APIError {
94
- override readonly status : undefined = undefined ;
95
-
95
+ export class APIUserAbortError extends APIError < undefined , undefined , undefined > {
96
96
constructor ( { message } : { message ?: string } = { } ) {
97
97
super ( undefined , undefined , message || 'Request was aborted.' , undefined ) ;
98
98
}
99
99
}
100
100
101
- export class APIConnectionError extends APIError {
102
- override readonly status : undefined = undefined ;
103
-
101
+ export class APIConnectionError extends APIError < undefined , undefined , undefined > {
104
102
constructor ( { message, cause } : { message ?: string | undefined ; cause ?: Error | undefined } ) {
105
103
super ( undefined , undefined , message || 'Connection error.' , undefined ) ;
106
104
// in some environments the 'cause' property is already declared
@@ -115,32 +113,18 @@ export class APIConnectionTimeoutError extends APIConnectionError {
115
113
}
116
114
}
117
115
118
- export class BadRequestError extends APIError {
119
- override readonly status : 400 = 400 ;
120
- }
116
+ export class BadRequestError extends APIError < 400 , Headers > { }
121
117
122
- export class AuthenticationError extends APIError {
123
- override readonly status : 401 = 401 ;
124
- }
118
+ export class AuthenticationError extends APIError < 401 , Headers > { }
125
119
126
- export class PermissionDeniedError extends APIError {
127
- override readonly status : 403 = 403 ;
128
- }
120
+ export class PermissionDeniedError extends APIError < 403 , Headers > { }
129
121
130
- export class NotFoundError extends APIError {
131
- override readonly status : 404 = 404 ;
132
- }
122
+ export class NotFoundError extends APIError < 404 , Headers > { }
133
123
134
- export class ConflictError extends APIError {
135
- override readonly status : 409 = 409 ;
136
- }
124
+ export class ConflictError extends APIError < 409 , Headers > { }
137
125
138
- export class UnprocessableEntityError extends APIError {
139
- override readonly status : 422 = 422 ;
140
- }
126
+ export class UnprocessableEntityError extends APIError < 422 , Headers > { }
141
127
142
- export class RateLimitError extends APIError {
143
- override readonly status : 429 = 429 ;
144
- }
128
+ export class RateLimitError extends APIError < 429 , Headers > { }
145
129
146
- export class InternalServerError extends APIError { }
130
+ export class InternalServerError extends APIError < number , Headers > { }
0 commit comments