Skip to content

Commit 7a96676

Browse files
committed
feat: Add ResponseStatusCodeError for handling 4xx and 5xx errors
- Introduced a new error type ResponseStatusCodeError to handle 4xx and 5xx HTTP response status codes. This is needed because we set throwOnError to true in all requests. - ResponseStatusCodeError includes `statusCode`, `headers`, and `body` properties.
1 parent f44db76 commit 7a96676

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

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

+16
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,19 @@ export class ConnectionRefusedError extends CustomError {
3232
super(`Connection to ${sanitizeUrl(url)} was refused`, cause);
3333
}
3434
}
35+
36+
export class ResponseStatusCodeError extends CustomError {
37+
public readonly statusCode: number;
38+
public readonly headers:
39+
| string[]
40+
| Record<string, string | string[] | undefined>
41+
| null;
42+
public readonly body: null | Record<string, any> | string;
43+
44+
constructor(url: string, cause: UndiciT.errors.ResponseStatusCodeError) {
45+
super(`Received an unexpected status code from ${sanitizeUrl(url)}`, cause);
46+
this.statusCode = cause.statusCode;
47+
this.headers = cause.headers;
48+
this.body = cause.body;
49+
}
50+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export async function getBaseRequestOptions(
3434
signal?: EventEmitter | AbortSignal | undefined;
3535
dispatcher: UndiciT.Dispatcher;
3636
headers: Record<string, string>;
37-
throwOnError: boolean;
37+
throwOnError: true;
3838
}> {
3939
const { Dispatcher } = await import("undici");
4040
const dispatcher =

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

+24-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
DispatcherError,
1414
RequestTimeoutError,
1515
ConnectionRefusedError,
16+
ResponseStatusCodeError,
1617
} from "./errors/request.js";
1718
import { move } from "./fs.js";
1819
import {
@@ -106,6 +107,11 @@ export async function getRequest(
106107
throw new RequestTimeoutError(url, e);
107108
}
108109

110+
if (e.code === "UND_ERR_RESPONSE_STATUS_CODE") {
111+
ensureError<UndiciT.errors.ResponseStatusCodeError>(e);
112+
throw new ResponseStatusCodeError(url, e);
113+
}
114+
109115
throw new RequestError(url, "GET", e);
110116
}
111117
}
@@ -160,6 +166,11 @@ export async function postJsonRequest(
160166
throw new RequestTimeoutError(url, e);
161167
}
162168

169+
if (e.code === "UND_ERR_RESPONSE_STATUS_CODE") {
170+
ensureError<UndiciT.errors.ResponseStatusCodeError>(e);
171+
throw new ResponseStatusCodeError(url, e);
172+
}
173+
163174
throw new RequestError(url, "POST", e);
164175
}
165176
}
@@ -215,6 +226,11 @@ export async function postFormRequest(
215226
throw new RequestTimeoutError(url, e);
216227
}
217228

229+
if (e.code === "UND_ERR_RESPONSE_STATUS_CODE") {
230+
ensureError<UndiciT.errors.ResponseStatusCodeError>(e);
231+
throw new ResponseStatusCodeError(url, e);
232+
}
233+
218234
throw new RequestError(url, "POST", e);
219235
}
220236
}
@@ -270,6 +286,11 @@ export async function download(
270286
throw new RequestTimeoutError(url, e);
271287
}
272288

289+
if (e.code === "UND_ERR_RESPONSE_STATUS_CODE") {
290+
ensureError<UndiciT.errors.ResponseStatusCodeError>(e);
291+
throw new ResponseStatusCodeError(url, e);
292+
}
293+
273294
throw new DownloadError(url, e);
274295
}
275296
}
@@ -373,9 +394,10 @@ export function isValidUrl(url: string): boolean {
373394
}
374395

375396
export {
397+
ConnectionRefusedError,
398+
DispatcherError,
376399
DownloadError,
377400
RequestError,
378-
DispatcherError,
379401
RequestTimeoutError,
380-
ConnectionRefusedError,
402+
ResponseStatusCodeError,
381403
} from "./errors/request.js";

0 commit comments

Comments
 (0)