-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathhardhat-error.ts
68 lines (56 loc) · 1.87 KB
/
hardhat-error.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import type { ErrorDescriptor } from "@ignored/hardhat-vnext-errors";
import assert from "node:assert/strict";
import { HardhatError } from "@ignored/hardhat-vnext-errors";
import { ensureError } from "@ignored/hardhat-vnext-utils/error";
export function assertIsHardhatError<ErrorDescriptorT extends ErrorDescriptor>(
error: unknown,
descritor: ErrorDescriptorT,
messageArguments: HardhatError<ErrorDescriptorT>["messageArguments"],
): asserts error is HardhatError<ErrorDescriptorT> {
assert.ok(HardhatError.isHardhatError(error), "Error is not a HardhatError");
// We first compare the number individually to throw a better error message
// in case the descriptors are different.
assert.equal(
error.descriptor.number,
descritor.number,
`Expected error number ${descritor.number}, but got ${error.descriptor.number}`,
);
assert.deepEqual(error.descriptor, descritor);
assert.deepEqual(error.messageArguments, messageArguments);
}
export function assertThrowsHardhatError<
ErrorDescriptorT extends ErrorDescriptor,
>(
f: () => any,
descritor: ErrorDescriptorT,
messageArguments: HardhatError<ErrorDescriptorT>["messageArguments"],
): void {
try {
f();
} catch (error) {
ensureError(error);
assertIsHardhatError(error, descritor, messageArguments);
return;
}
assert.fail("Function did not throw any error");
}
export async function assertRejectsWithHardhatError<
ErrorDescriptorT extends ErrorDescriptor,
>(
f: (() => Promise<any>) | Promise<any>,
descritor: ErrorDescriptorT,
messageArguments: HardhatError<ErrorDescriptorT>["messageArguments"],
): Promise<void> {
try {
if (f instanceof Promise) {
await f;
} else {
await f();
}
} catch (error) {
ensureError(error);
assertIsHardhatError(error, descritor, messageArguments);
return;
}
assert.fail("Function did not throw any error");
}