Skip to content

Commit 945e73d

Browse files
manuelwedlerschaable
authored andcommitted
Remove throwOnError request parameter and adapt Sourcify response handling logic
1 parent 91113bc commit 945e73d

3 files changed

Lines changed: 71 additions & 44 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ export async function generateTempFilePath(filePath: string): Promise<string> {
3030

3131
export async function getBaseRequestOptions(
3232
requestUrl: string,
33-
{ extraHeaders, abortSignal, queryParams, throwOnError }: RequestOptions = {},
33+
{ extraHeaders, abortSignal, queryParams }: RequestOptions = {},
3434
dispatcherOrDispatcherOptions?: UndiciT.Dispatcher | DispatcherOptions,
3535
): Promise<{
3636
query?: Record<string, any> | undefined;
3737
signal?: EventEmitter | AbortSignal | undefined;
3838
dispatcher: UndiciT.Dispatcher;
3939
headers: Record<string, string>;
40-
throwOnError: boolean;
40+
throwOnError: true;
4141
}> {
4242
const { Dispatcher } = await import("undici");
4343
const dispatcher =
@@ -51,7 +51,7 @@ export async function getBaseRequestOptions(
5151
return {
5252
dispatcher,
5353
headers: getHeaders(requestUrl, extraHeaders),
54-
throwOnError: throwOnError ?? true,
54+
throwOnError: true,
5555
...(abortSignal !== undefined ? { signal: abortSignal } : {}),
5656
...(queryParams !== undefined ? { query: queryParams } : {}),
5757
};

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ export interface RequestOptions {
6060
queryParams?: Record<string, any>;
6161
extraHeaders?: Record<string, string>;
6262
abortSignal?: AbortSignal | EventEmitter;
63-
throwOnError?: boolean;
6463
}
6564

6665
export interface HttpResponse {

v-next/hardhat-verify/src/internal/sourcify.ts

Lines changed: 68 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
getProxyUrl,
2424
getRequest,
2525
postJsonRequest,
26+
ResponseStatusCodeError,
2627
shouldUseProxy,
2728
} from "@nomicfoundation/hardhat-utils/request";
2829

@@ -81,12 +82,36 @@ export class Sourcify implements VerificationProvider {
8182
try {
8283
response = await getRequest(
8384
`${this.apiUrl}/v2/contract/${this.chainId}/${address}`,
84-
{ throwOnError: false },
85+
{},
8586
this.dispatcherOrDispatcherOptions,
8687
);
8788
responseBody = await response.body.json();
8889
} catch (error) {
8990
ensureError(error);
91+
92+
if (
93+
error instanceof ResponseStatusCodeError &&
94+
isSourcifyLookupResponse(error?.body)
95+
) {
96+
// Unverified contracts are returned with status 404
97+
return error.body.match !== null;
98+
}
99+
100+
if (
101+
error instanceof ResponseStatusCodeError &&
102+
isSourcifyErrorResponse(error?.body)
103+
) {
104+
throw new HardhatError(
105+
HardhatError.ERRORS.HARDHAT_VERIFY.GENERAL.EXPLORER_REQUEST_STATUS_CODE_ERROR,
106+
{
107+
name: this.name,
108+
url: this.apiUrl,
109+
statusCode: error.statusCode,
110+
errorMessage: error.body.message,
111+
},
112+
);
113+
}
114+
90115
throw new HardhatError(
91116
HardhatError.ERRORS.HARDHAT_VERIFY.GENERAL.EXPLORER_REQUEST_FAILED,
92117
{
@@ -98,18 +123,6 @@ export class Sourcify implements VerificationProvider {
98123
);
99124
}
100125

101-
if (isSourcifyErrorResponse(responseBody)) {
102-
throw new HardhatError(
103-
HardhatError.ERRORS.HARDHAT_VERIFY.GENERAL.EXPLORER_REQUEST_STATUS_CODE_ERROR,
104-
{
105-
name: this.name,
106-
url: this.apiUrl,
107-
statusCode: response.statusCode,
108-
errorMessage: responseBody.message,
109-
},
110-
);
111-
}
112-
113126
if (!isSourcifyLookupResponse(responseBody)) {
114127
throw new HardhatError(
115128
HardhatError.ERRORS.HARDHAT_VERIFY.GENERAL.CONTRACT_VERIFICATION_UNEXPECTED_RESPONSE,
@@ -149,12 +162,33 @@ export class Sourcify implements VerificationProvider {
149162
response = await postJsonRequest(
150163
`${this.apiUrl}/v2/verify/${this.chainId}/${contractAddress}`,
151164
body,
152-
{ throwOnError: false },
165+
{},
153166
this.dispatcherOrDispatcherOptions,
154167
);
155168
responseBody = await response.body.json();
156169
} catch (error) {
157170
ensureError(error);
171+
172+
if (
173+
error instanceof ResponseStatusCodeError &&
174+
isSourcifyErrorResponse(error?.body)
175+
) {
176+
if (error.body.customCode === "already_verified") {
177+
throw new HardhatError(
178+
HardhatError.ERRORS.HARDHAT_VERIFY.GENERAL.CONTRACT_ALREADY_VERIFIED,
179+
{
180+
contract: contractName,
181+
address: contractAddress,
182+
},
183+
);
184+
}
185+
186+
throw new HardhatError(
187+
HardhatError.ERRORS.HARDHAT_VERIFY.GENERAL.CONTRACT_VERIFICATION_REQUEST_FAILED,
188+
{ message: error.body.message },
189+
);
190+
}
191+
158192
throw new HardhatError(
159193
HardhatError.ERRORS.HARDHAT_VERIFY.GENERAL.EXPLORER_REQUEST_FAILED,
160194
{
@@ -166,23 +200,6 @@ export class Sourcify implements VerificationProvider {
166200
);
167201
}
168202

169-
if (isSourcifyErrorResponse(responseBody)) {
170-
if (responseBody.customCode === "already_verified") {
171-
throw new HardhatError(
172-
HardhatError.ERRORS.HARDHAT_VERIFY.GENERAL.CONTRACT_ALREADY_VERIFIED,
173-
{
174-
contract: contractName,
175-
address: contractAddress,
176-
},
177-
);
178-
}
179-
180-
throw new HardhatError(
181-
HardhatError.ERRORS.HARDHAT_VERIFY.GENERAL.CONTRACT_VERIFICATION_REQUEST_FAILED,
182-
{ message: responseBody.message },
183-
);
184-
}
185-
186203
if (!isSourcifyVerificationResponse(responseBody)) {
187204
throw new HardhatError(
188205
HardhatError.ERRORS.HARDHAT_VERIFY.GENERAL.CONTRACT_VERIFICATION_UNEXPECTED_RESPONSE,
@@ -208,12 +225,23 @@ export class Sourcify implements VerificationProvider {
208225
try {
209226
response = await getRequest(
210227
`${this.apiUrl}/v2/verify/${guid}`,
211-
{ throwOnError: false },
228+
{},
212229
this.dispatcherOrDispatcherOptions,
213230
);
214231
responseBody = await response.body.json();
215232
} catch (error) {
216233
ensureError(error);
234+
235+
if (
236+
error instanceof ResponseStatusCodeError &&
237+
isSourcifyErrorResponse(error?.body)
238+
) {
239+
throw new HardhatError(
240+
HardhatError.ERRORS.HARDHAT_VERIFY.GENERAL.CONTRACT_VERIFICATION_STATUS_POLLING_FAILED,
241+
{ message: error.body.message },
242+
);
243+
}
244+
217245
throw new HardhatError(
218246
HardhatError.ERRORS.HARDHAT_VERIFY.GENERAL.EXPLORER_REQUEST_FAILED,
219247
{
@@ -225,13 +253,6 @@ export class Sourcify implements VerificationProvider {
225253
);
226254
}
227255

228-
if (isSourcifyErrorResponse(responseBody)) {
229-
throw new HardhatError(
230-
HardhatError.ERRORS.HARDHAT_VERIFY.GENERAL.CONTRACT_VERIFICATION_STATUS_POLLING_FAILED,
231-
{ message: responseBody.message },
232-
);
233-
}
234-
235256
if (!isSourcifyVerificationStatusResponse(responseBody)) {
236257
throw new HardhatError(
237258
HardhatError.ERRORS.HARDHAT_VERIFY.GENERAL.CONTRACT_VERIFICATION_UNEXPECTED_RESPONSE,
@@ -293,6 +314,7 @@ function isSourcifyErrorResponse(
293314
): response is SourcifyErrorResponse {
294315
return (
295316
typeof response === "object" &&
317+
response !== null &&
296318
"customCode" in response &&
297319
"message" in response &&
298320
"errorId" in response
@@ -304,6 +326,7 @@ function isSourcifyLookupResponse(
304326
): response is SourcifyLookupResponse {
305327
return (
306328
typeof response === "object" &&
329+
response !== null &&
307330
"match" in response &&
308331
"creationMatch" in response &&
309332
"runtimeMatch" in response &&
@@ -315,14 +338,19 @@ function isSourcifyLookupResponse(
315338
function isSourcifyVerificationResponse(
316339
response: any,
317340
): response is SourcifyVerificationResponse {
318-
return typeof response === "object" && "verificationId" in response;
341+
return (
342+
typeof response === "object" &&
343+
response !== null &&
344+
"verificationId" in response
345+
);
319346
}
320347

321348
function isSourcifyVerificationStatusResponse(
322349
response: any,
323350
): response is SourcifyVerificationStatusResponse {
324351
return (
325352
typeof response === "object" &&
353+
response !== null &&
326354
"isJobCompleted" in response &&
327355
"verificationId" in response &&
328356
"jobStartTime" in response &&

0 commit comments

Comments
 (0)