@@ -11,6 +11,8 @@ import {
11
11
DownloadError ,
12
12
RequestError ,
13
13
DispatcherError ,
14
+ RequestTimeoutError ,
15
+ ConnectionRefusedError ,
14
16
} from "./errors/request.js" ;
15
17
import { move } from "./fs.js" ;
16
18
import {
@@ -66,7 +68,9 @@ export interface RequestOptions {
66
68
* @param requestOptions The options to configure the request. See {@link RequestOptions}.
67
69
* @param dispatcherOrDispatcherOptions Either a dispatcher or dispatcher options. See {@link DispatcherOptions}.
68
70
* @returns The response data object. See {@link https://undici.nodejs.org/#/docs/api/Dispatcher?id=parameter-responsedata}.
69
- * @throws RequestError If the request fails.
71
+ * @throws ConnectionRefusedError If the connection is refused by the server.
72
+ * @throws RequestTimeoutError If the request times out.
73
+ * @throws RequestError If the request fails for any other reason.
70
74
*/
71
75
export async function getRequest (
72
76
url : string ,
@@ -86,7 +90,20 @@ export async function getRequest(
86
90
...baseRequestOptions ,
87
91
} ) ;
88
92
} catch ( e ) {
89
- ensureError ( e ) ;
93
+ ensureError < NodeJS . ErrnoException > ( e ) ;
94
+
95
+ if ( e . code === "ECONNREFUSED" ) {
96
+ throw new ConnectionRefusedError ( url , e ) ;
97
+ }
98
+
99
+ if (
100
+ e . code === "UND_ERR_CONNECT_TIMEOUT" ||
101
+ e . code === "UND_ERR_HEADERS_TIMEOUT" ||
102
+ e . code === "UND_ERR_BODY_TIMEOUT"
103
+ ) {
104
+ throw new RequestTimeoutError ( url , e ) ;
105
+ }
106
+
90
107
throw new RequestError ( url , "GET" , e ) ;
91
108
}
92
109
}
@@ -99,7 +116,9 @@ export async function getRequest(
99
116
* @param requestOptions The options to configure the request. See {@link RequestOptions}.
100
117
* @param dispatcherOrDispatcherOptions Either a dispatcher or dispatcher options. See {@link DispatcherOptions}.
101
118
* @returns The response data object. See {@link https://undici.nodejs.org/#/docs/api/Dispatcher?id=parameter-responsedata}.
102
- * @throws RequestError If the request fails.
119
+ * @throws ConnectionRefusedError If the connection is refused by the server.
120
+ * @throws RequestTimeoutError If the request times out.
121
+ * @throws RequestError If the request fails for any other reason.
103
122
*/
104
123
export async function postJsonRequest (
105
124
url : string ,
@@ -125,7 +144,20 @@ export async function postJsonRequest(
125
144
body : JSON . stringify ( body ) ,
126
145
} ) ;
127
146
} catch ( e ) {
128
- ensureError ( e ) ;
147
+ ensureError < NodeJS . ErrnoException > ( e ) ;
148
+
149
+ if ( e . code === "ECONNREFUSED" ) {
150
+ throw new ConnectionRefusedError ( url , e ) ;
151
+ }
152
+
153
+ if (
154
+ e . code === "UND_ERR_CONNECT_TIMEOUT" ||
155
+ e . code === "UND_ERR_HEADERS_TIMEOUT" ||
156
+ e . code === "UND_ERR_BODY_TIMEOUT"
157
+ ) {
158
+ throw new RequestTimeoutError ( url , e ) ;
159
+ }
160
+
129
161
throw new RequestError ( url , "POST" , e ) ;
130
162
}
131
163
}
@@ -138,7 +170,9 @@ export async function postJsonRequest(
138
170
* @param requestOptions The options to configure the request. See {@link RequestOptions}.
139
171
* @param dispatcherOrDispatcherOptions Either a dispatcher or dispatcher options. See {@link DispatcherOptions}.
140
172
* @returns The response data object. See {@link https://undici.nodejs.org/#/docs/api/Dispatcher?id=parameter-responsedata}.
141
- * @throws RequestError If the request fails.
173
+ * @throws ConnectionRefusedError If the connection is refused by the server.
174
+ * @throws RequestTimeoutError If the request times out.
175
+ * @throws RequestError If the request fails for any other reason.
142
176
*/
143
177
export async function postFormRequest (
144
178
url : string ,
@@ -165,7 +199,20 @@ export async function postFormRequest(
165
199
body : querystring . stringify ( body as ParsedUrlQueryInput ) ,
166
200
} ) ;
167
201
} catch ( e ) {
168
- ensureError ( e ) ;
202
+ ensureError < NodeJS . ErrnoException > ( e ) ;
203
+
204
+ if ( e . code === "ECONNREFUSED" ) {
205
+ throw new ConnectionRefusedError ( url , e ) ;
206
+ }
207
+
208
+ if (
209
+ e . code === "UND_ERR_CONNECT_TIMEOUT" ||
210
+ e . code === "UND_ERR_HEADERS_TIMEOUT" ||
211
+ e . code === "UND_ERR_BODY_TIMEOUT"
212
+ ) {
213
+ throw new RequestTimeoutError ( url , e ) ;
214
+ }
215
+
169
216
throw new RequestError ( url , "POST" , e ) ;
170
217
}
171
218
}
@@ -177,7 +224,9 @@ export async function postFormRequest(
177
224
* @param destination The absolute path to save the file to.
178
225
* @param requestOptions The options to configure the request. See {@link RequestOptions}.
179
226
* @param dispatcherOrDispatcherOptions Either a dispatcher or dispatcher options. See {@link DispatcherOptions}.
180
- * @throws DownloadFailedError If the download fails.
227
+ * @throws ConnectionRefusedError If the connection is refused by the server.
228
+ * @throws RequestTimeoutError If the request times out.
229
+ * @throws DownloadFailedError If the download fails for any other reason.
181
230
*/
182
231
export async function download (
183
232
url : string ,
@@ -205,7 +254,20 @@ export async function download(
205
254
await stream . pipeline ( body , fileStream ) ;
206
255
await move ( tempFilePath , destination ) ;
207
256
} catch ( e ) {
208
- ensureError ( e ) ;
257
+ ensureError < NodeJS . ErrnoException > ( e ) ;
258
+
259
+ if ( e . code === "ECONNREFUSED" ) {
260
+ throw new ConnectionRefusedError ( url , e ) ;
261
+ }
262
+
263
+ if (
264
+ e . code === "UND_ERR_CONNECT_TIMEOUT" ||
265
+ e . code === "UND_ERR_HEADERS_TIMEOUT" ||
266
+ e . code === "UND_ERR_BODY_TIMEOUT"
267
+ ) {
268
+ throw new RequestTimeoutError ( url , e ) ;
269
+ }
270
+
209
271
throw new DownloadError ( url , e ) ;
210
272
}
211
273
}
@@ -301,4 +363,6 @@ export {
301
363
DownloadError ,
302
364
RequestError ,
303
365
DispatcherError ,
366
+ RequestTimeoutError ,
367
+ ConnectionRefusedError ,
304
368
} from "./errors/request.js" ;
0 commit comments