@@ -11,9 +11,6 @@ import {
11
11
DownloadError ,
12
12
RequestError ,
13
13
DispatcherError ,
14
- RequestTimeoutError ,
15
- ConnectionRefusedError ,
16
- ResponseStatusCodeError ,
17
14
} from "./errors/request.js" ;
18
15
import { move } from "./fs.js" ;
19
16
import {
@@ -23,6 +20,7 @@ import {
23
20
getBasicDispatcher ,
24
21
getPoolDispatcher ,
25
22
getProxyDispatcher ,
23
+ handleError ,
26
24
} from "./internal/request.js" ;
27
25
28
26
export const DEFAULT_TIMEOUT_IN_MILLISECONDS = 30_000 ;
@@ -95,22 +93,7 @@ export async function getRequest(
95
93
} catch ( e ) {
96
94
ensureError < NodeJS . ErrnoException > ( e ) ;
97
95
98
- if ( e . code === "ECONNREFUSED" ) {
99
- throw new ConnectionRefusedError ( url , e ) ;
100
- }
101
-
102
- if (
103
- e . code === "UND_ERR_CONNECT_TIMEOUT" ||
104
- e . code === "UND_ERR_HEADERS_TIMEOUT" ||
105
- e . code === "UND_ERR_BODY_TIMEOUT"
106
- ) {
107
- throw new RequestTimeoutError ( url , e ) ;
108
- }
109
-
110
- if ( e . code === "UND_ERR_RESPONSE_STATUS_CODE" ) {
111
- ensureError < UndiciT . errors . ResponseStatusCodeError > ( e ) ;
112
- throw new ResponseStatusCodeError ( url , e ) ;
113
- }
96
+ handleError ( e , url ) ;
114
97
115
98
throw new RequestError ( url , "GET" , e ) ;
116
99
}
@@ -154,22 +137,7 @@ export async function postJsonRequest(
154
137
} catch ( e ) {
155
138
ensureError < NodeJS . ErrnoException > ( e ) ;
156
139
157
- if ( e . code === "ECONNREFUSED" ) {
158
- throw new ConnectionRefusedError ( url , e ) ;
159
- }
160
-
161
- if (
162
- e . code === "UND_ERR_CONNECT_TIMEOUT" ||
163
- e . code === "UND_ERR_HEADERS_TIMEOUT" ||
164
- e . code === "UND_ERR_BODY_TIMEOUT"
165
- ) {
166
- throw new RequestTimeoutError ( url , e ) ;
167
- }
168
-
169
- if ( e . code === "UND_ERR_RESPONSE_STATUS_CODE" ) {
170
- ensureError < UndiciT . errors . ResponseStatusCodeError > ( e ) ;
171
- throw new ResponseStatusCodeError ( url , e ) ;
172
- }
140
+ handleError ( e , url ) ;
173
141
174
142
throw new RequestError ( url , "POST" , e ) ;
175
143
}
@@ -214,22 +182,7 @@ export async function postFormRequest(
214
182
} catch ( e ) {
215
183
ensureError < NodeJS . ErrnoException > ( e ) ;
216
184
217
- if ( e . code === "ECONNREFUSED" ) {
218
- throw new ConnectionRefusedError ( url , e ) ;
219
- }
220
-
221
- if (
222
- e . code === "UND_ERR_CONNECT_TIMEOUT" ||
223
- e . code === "UND_ERR_HEADERS_TIMEOUT" ||
224
- e . code === "UND_ERR_BODY_TIMEOUT"
225
- ) {
226
- throw new RequestTimeoutError ( url , e ) ;
227
- }
228
-
229
- if ( e . code === "UND_ERR_RESPONSE_STATUS_CODE" ) {
230
- ensureError < UndiciT . errors . ResponseStatusCodeError > ( e ) ;
231
- throw new ResponseStatusCodeError ( url , e ) ;
232
- }
185
+ handleError ( e , url ) ;
233
186
234
187
throw new RequestError ( url , "POST" , e ) ;
235
188
}
@@ -274,22 +227,7 @@ export async function download(
274
227
} catch ( e ) {
275
228
ensureError < NodeJS . ErrnoException > ( e ) ;
276
229
277
- if ( e . code === "ECONNREFUSED" ) {
278
- throw new ConnectionRefusedError ( url , e ) ;
279
- }
280
-
281
- if (
282
- e . code === "UND_ERR_CONNECT_TIMEOUT" ||
283
- e . code === "UND_ERR_HEADERS_TIMEOUT" ||
284
- e . code === "UND_ERR_BODY_TIMEOUT"
285
- ) {
286
- throw new RequestTimeoutError ( url , e ) ;
287
- }
288
-
289
- if ( e . code === "UND_ERR_RESPONSE_STATUS_CODE" ) {
290
- ensureError < UndiciT . errors . ResponseStatusCodeError > ( e ) ;
291
- throw new ResponseStatusCodeError ( url , e ) ;
292
- }
230
+ handleError ( e , url ) ;
293
231
294
232
throw new DownloadError ( url , e ) ;
295
233
}
0 commit comments