Skip to content

Commit 7d2764f

Browse files
committed
refactor: Extract error handling logic to a separate function and fix tests
- Moved error handling logic to a handleError function to avoid code duplication. - Updated the error handling to correctly check for e.cause.code or e.code existence. - Fixed tests that were not previously updated to expect ResponseStatusCodeError.
1 parent f532124 commit 7d2764f

File tree

3 files changed

+46
-72
lines changed

3 files changed

+46
-72
lines changed

v-next/hardhat-utils/src/internal/request.ts

+33
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ import type UndiciT from "undici";
55
import path from "node:path";
66
import url from "node:url";
77

8+
import { ensureError } from "../error.js";
89
import { mkdir } from "../fs.js";
910
import {
11+
ConnectionRefusedError,
1012
DEFAULT_MAX_REDIRECTS,
1113
DEFAULT_TIMEOUT_IN_MILLISECONDS,
1214
DEFAULT_USER_AGENT,
1315
getDispatcher,
16+
RequestTimeoutError,
17+
ResponseStatusCodeError,
1418
} from "../request.js";
1519

1620
export async function generateTempFilePath(filePath: string): Promise<string> {
@@ -135,3 +139,32 @@ export function sanitizeUrl(requestUrl: string): string {
135139
const parsedUrl = new URL(requestUrl);
136140
return url.format(parsedUrl, { auth: false, search: false, fragment: false });
137141
}
142+
143+
export function handleError(
144+
e: NodeJS.ErrnoException,
145+
requestUrl: string,
146+
): void {
147+
let causeCode;
148+
if (e.cause !== undefined) {
149+
ensureError<NodeJS.ErrnoException>(e.cause);
150+
causeCode = e.cause.code;
151+
}
152+
const errorCode = e.code ?? causeCode;
153+
154+
if (errorCode === "ECONNREFUSED") {
155+
throw new ConnectionRefusedError(requestUrl, e);
156+
}
157+
158+
if (
159+
errorCode === "UND_ERR_CONNECT_TIMEOUT" ||
160+
errorCode === "UND_ERR_HEADERS_TIMEOUT" ||
161+
errorCode === "UND_ERR_BODY_TIMEOUT"
162+
) {
163+
throw new RequestTimeoutError(requestUrl, e);
164+
}
165+
166+
if (errorCode === "UND_ERR_RESPONSE_STATUS_CODE") {
167+
ensureError<UndiciT.errors.ResponseStatusCodeError>(e);
168+
throw new ResponseStatusCodeError(requestUrl, e);
169+
}
170+
}

v-next/hardhat-utils/src/request.ts

+5-64
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
getBasicDispatcher,
2424
getPoolDispatcher,
2525
getProxyDispatcher,
26+
handleError,
2627
} from "./internal/request.js";
2728

2829
export const DEFAULT_TIMEOUT_IN_MILLISECONDS = 30_000;
@@ -95,22 +96,7 @@ export async function getRequest(
9596
} catch (e) {
9697
ensureError<NodeJS.ErrnoException>(e);
9798

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-
}
99+
handleError(e, url);
114100

115101
throw new RequestError(url, "GET", e);
116102
}
@@ -154,22 +140,7 @@ export async function postJsonRequest(
154140
} catch (e) {
155141
ensureError<NodeJS.ErrnoException>(e);
156142

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-
}
143+
handleError(e, url);
173144

174145
throw new RequestError(url, "POST", e);
175146
}
@@ -214,22 +185,7 @@ export async function postFormRequest(
214185
} catch (e) {
215186
ensureError<NodeJS.ErrnoException>(e);
216187

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-
}
188+
handleError(e, url);
233189

234190
throw new RequestError(url, "POST", e);
235191
}
@@ -274,22 +230,7 @@ export async function download(
274230
} catch (e) {
275231
ensureError<NodeJS.ErrnoException>(e);
276232

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-
}
233+
handleError(e, url);
293234

294235
throw new DownloadError(url, e);
295236
}

v-next/hardhat-utils/test/request.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ describe("Requests util", () => {
309309
.reply(500, "Internal Server Error");
310310

311311
await assert.rejects(getRequest(url, undefined, interceptor), {
312-
name: "RequestError",
313-
message: `Failed to make GET request to ${url}`,
312+
name: "ResponseStatusCodeError",
313+
message: `Received an unexpected status code from ${url}`,
314314
});
315315
});
316316
});
@@ -407,8 +407,8 @@ describe("Requests util", () => {
407407
.reply(500, "Internal Server Error");
408408

409409
await assert.rejects(postJsonRequest(url, body, undefined, interceptor), {
410-
name: "RequestError",
411-
message: `Failed to make POST request to ${url}`,
410+
name: "ResponseStatusCodeError",
411+
message: `Received an unexpected status code from ${url}`,
412412
});
413413
});
414414
});
@@ -505,8 +505,8 @@ describe("Requests util", () => {
505505
.reply(500, "Internal Server Error");
506506

507507
await assert.rejects(postFormRequest(url, body, undefined, interceptor), {
508-
name: "RequestError",
509-
message: `Failed to make POST request to ${url}`,
508+
name: "ResponseStatusCodeError",
509+
message: `Received an unexpected status code from ${url}`,
510510
});
511511
});
512512
});
@@ -539,8 +539,8 @@ describe("Requests util", () => {
539539
.reply(500, "Internal Server Error");
540540

541541
await assert.rejects(download(url, destination, undefined, interceptor), {
542-
name: "DownloadError",
543-
message: `Failed to download file from ${url}`,
542+
name: "ResponseStatusCodeError",
543+
message: `Received an unexpected status code from ${url}`,
544544
});
545545
});
546546
});

0 commit comments

Comments
 (0)