Skip to content

Commit 0587b6b

Browse files
committed
Fix tests and improve types
1 parent 76674de commit 0587b6b

File tree

3 files changed

+28
-48
lines changed

3 files changed

+28
-48
lines changed

v-next/hardhat/src/internal/cli/error-handler.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ interface ErrorMessages {
6565
* `console.error`. Useful for testing to capture error messages.
6666
*/
6767
export function printErrorMessages(
68-
error: unknown,
68+
error: Error,
6969
shouldShowStackTraces: boolean = false,
70-
print: (message: any) => void = console.error,
70+
print: (message: string | Error) => void = console.error,
7171
): void {
7272
const showStackTraces =
7373
shouldShowStackTraces ||
@@ -93,7 +93,7 @@ export function printErrorMessages(
9393
}
9494
}
9595

96-
function getErrorWithCategory(error: unknown): ErrorWithCategory {
96+
function getErrorWithCategory(error: Error): ErrorWithCategory {
9797
if (HardhatError.isHardhatError(error)) {
9898
if (error.pluginId === undefined) {
9999
return {
@@ -121,7 +121,7 @@ function getErrorWithCategory(error: unknown): ErrorWithCategory {
121121
};
122122
}
123123

124-
function getErrorMessages(error: unknown): ErrorMessages {
124+
function getErrorMessages(error: Error): ErrorMessages {
125125
const { category, categorizedError } = getErrorWithCategory(error);
126126
switch (category) {
127127
case ErrorCategory.HARDHAT:

v-next/hardhat/src/internal/cli/main.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
assertHardhatInvariant,
1111
} from "@nomicfoundation/hardhat-errors";
1212
import { isCi } from "@nomicfoundation/hardhat-utils/ci";
13+
import { ensureError } from "@nomicfoundation/hardhat-utils/error";
1314
import { readClosestPackageJson } from "@nomicfoundation/hardhat-utils/package";
1415
import { kebabToCamelCase } from "@nomicfoundation/hardhat-utils/string";
1516
import debug from "debug";
@@ -201,14 +202,13 @@ export async function main(
201202

202203
await Promise.all([task.run(taskArguments), sendTaskAnalytics(task.id)]);
203204
} catch (error) {
205+
ensureError(error);
204206
printErrorMessages(error, builtinGlobalOptions?.showStackTraces);
205207

206-
if (error instanceof Error) {
207-
try {
208-
await sendErrorTelemetry(error);
209-
} catch (e) {
210-
log("Couldn't report error to sentry: %O", e);
211-
}
208+
try {
209+
await sendErrorTelemetry(error);
210+
} catch (e) {
211+
log("Couldn't report error to sentry: %O", e);
212212
}
213213

214214
if (options.rethrowErrors) {

v-next/hardhat/test/internal/cli/error-handler.ts

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import assert from "node:assert/strict";
22
import { describe, it } from "node:test";
3-
import util from "node:util";
43

54
import {
65
HardhatError,
@@ -32,10 +31,10 @@ describe("error-handler", () => {
3231
describe("printErrorMessages", () => {
3332
describe("with a Hardhat error", () => {
3433
it("should print the error message", () => {
35-
const lines: string[] = [];
34+
const lines: Array<string | Error> = [];
3635
const error = new HardhatError(mockCoreErrorDescriptor);
3736

38-
printErrorMessages(error, false, (msg: string) => {
37+
printErrorMessages(error, false, (msg: string | Error) => {
3938
lines.push(msg);
4039
});
4140

@@ -53,10 +52,10 @@ describe("error-handler", () => {
5352
});
5453

5554
it("should print the stack trace", () => {
56-
const lines: string[] = [];
55+
const lines: Array<string | Error> = [];
5756
const error = new HardhatError(mockCoreErrorDescriptor);
5857

59-
printErrorMessages(error, true, (msg: string) => {
58+
printErrorMessages(error, true, (msg: string | Error) => {
6059
lines.push(msg);
6160
});
6261

@@ -66,16 +65,16 @@ describe("error-handler", () => {
6665
`${chalk.red.bold(`Error ${error.errorCode}:`)} ${error.formattedMessage}`,
6766
);
6867
assert.equal(lines[1], "");
69-
assert.equal(lines[2], `${error.stack}`);
68+
assert.equal(lines[2], error);
7069
});
7170
});
7271

7372
describe("with a Hardhat plugin error", () => {
7473
it("should print the error message", () => {
75-
const lines: string[] = [];
74+
const lines: Array<string | Error> = [];
7675
const error = new HardhatError(mockPluginErrorDescriptor);
7776

78-
printErrorMessages(error, false, (msg: string) => {
77+
printErrorMessages(error, false, (msg: string | Error) => {
7978
lines.push(msg);
8079
});
8180

@@ -93,10 +92,10 @@ describe("error-handler", () => {
9392
});
9493

9594
it("should print the stack trace", () => {
96-
const lines: string[] = [];
95+
const lines: Array<string | Error> = [];
9796
const error = new HardhatError(mockPluginErrorDescriptor);
9897

99-
printErrorMessages(error, true, (msg: string) => {
98+
printErrorMessages(error, true, (msg: string | Error) => {
10099
lines.push(msg);
101100
});
102101

@@ -106,19 +105,19 @@ describe("error-handler", () => {
106105
`${chalk.red.bold(`Error ${error.errorCode} in plugin ${error.pluginId}:`)} ${error.formattedMessage}`,
107106
);
108107
assert.equal(lines[1], "");
109-
assert.equal(lines[2], `${error.stack}`);
108+
assert.equal(lines[2], error);
110109
});
111110
});
112111

113112
describe("with a Hardhat community plugin error", () => {
114113
it("should print the error message", () => {
115-
const lines: string[] = [];
114+
const lines: Array<string | Error> = [];
116115
const error = new HardhatPluginError(
117116
"community-plugin",
118117
"error message",
119118
);
120119

121-
printErrorMessages(error, false, (msg: string) => {
120+
printErrorMessages(error, false, (msg: string | Error) => {
122121
lines.push(msg);
123122
});
124123

@@ -135,13 +134,13 @@ describe("error-handler", () => {
135134
});
136135

137136
it("should print the stack trace", () => {
138-
const lines: string[] = [];
137+
const lines: Array<string | Error> = [];
139138
const error = new HardhatPluginError(
140139
"community-plugin",
141140
"error message",
142141
);
143142

144-
printErrorMessages(error, true, (msg: string) => {
143+
printErrorMessages(error, true, (msg: string | Error) => {
145144
lines.push(msg);
146145
});
147146

@@ -151,42 +150,23 @@ describe("error-handler", () => {
151150
`${chalk.red.bold(`Error in community plugin ${error.pluginId}:`)} ${error.message}`,
152151
);
153152
assert.equal(lines[1], "");
154-
assert.equal(lines[2], `${error.stack}`);
153+
assert.equal(lines[2], error);
155154
});
156155
});
157156

158157
describe("with an unknown error", () => {
159158
it("should print the error message with the stack traces for an instance of Error", () => {
160-
const lines: string[] = [];
159+
const lines: Array<string | Error> = [];
161160
const error = new Error("error message");
162161

163-
printErrorMessages(error, false, (msg: string) => {
162+
printErrorMessages(error, false, (msg: string | Error) => {
164163
lines.push(msg);
165164
});
166165

167166
assert.equal(lines.length, 5);
168167
assert.equal(lines[0], chalk.red.bold(`An unexpected error occurred:`));
169168
assert.equal(lines[1], "");
170-
assert.equal(lines[2], `${error.stack}`);
171-
assert.equal(lines[3], "");
172-
assert.equal(
173-
lines[4],
174-
`If you think this is a bug in Hardhat, please report it here: ${HARDHAT_WEBSITE_URL}report-bug`,
175-
);
176-
});
177-
178-
it("should print the error message with the error for an unknown error", () => {
179-
const lines: string[] = [];
180-
const error = { message: "error message" };
181-
182-
printErrorMessages(error, false, (msg: string) => {
183-
lines.push(msg);
184-
});
185-
186-
assert.equal(lines.length, 5);
187-
assert.equal(lines[0], chalk.red.bold(`An unexpected error occurred:`));
188-
assert.equal(lines[1], "");
189-
assert.equal(lines[2], `${util.inspect(error)}`);
169+
assert.equal(lines[2], error);
190170
assert.equal(lines[3], "");
191171
assert.equal(
192172
lines[4],

0 commit comments

Comments
 (0)