-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy patherror-handler.ts
146 lines (134 loc) · 4.7 KB
/
error-handler.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import {
HardhatError,
HardhatPluginError,
} from "@ignored/hardhat-vnext-errors";
import { isCi } from "@ignored/hardhat-vnext-utils/ci";
import chalk from "chalk";
import { HARDHAT_NAME, HARDHAT_WEBSITE_URL } from "../constants.js";
/**
* The different categories of errors that can be handled by hardhat cli.
* Each category has a different way of being formatted and displayed.
* To add new categories, add a new entry to this enum and update the
* {@link getErrorWithCategory} and {@link getErrorMessages} functions
* accordingly.
*/
enum ErrorCategory {
HARDHAT = "HARDHAT",
PLUGIN = "PLUGIN",
COMMUNITY_PLUGIN = "COMMUNITY_PLUGIN",
OTHER = "OTHER",
}
type ErrorWithCategory =
| {
category: ErrorCategory.HARDHAT;
categorizedError: HardhatError;
}
| {
category: ErrorCategory.PLUGIN;
categorizedError: HardhatError;
}
| {
category: ErrorCategory.COMMUNITY_PLUGIN;
categorizedError: HardhatPluginError;
}
| {
category: ErrorCategory.OTHER;
categorizedError: unknown;
};
/**
* The different messages that can be displayed for each category of errors.
* - `formattedErrorMessage`: the main error message that is always displayed.
* - `showMoreInfoMessage`: an optional message that can be displayed to
* provide more information about the error. It is only displayed when stack
* traces are hidden.
* - `postErrorStackTraceMessage` an optional message that can be displayed
* after the stack trace. It is only displayed when stack traces are shown.
*/
interface ErrorMessages {
formattedErrorMessage: string;
showMoreInfoMessage?: string;
postErrorStackTraceMessage?: string;
}
/**
* Formats and logs error messages based on the category the error belongs to.
*
* @param error the error to handle. Supported categories are defined in
* {@link ErrorCategory}.
* @param print the function used to print the error message, defaults to
* `console.error`. Useful for testing to capture error messages.
*/
export function printErrorMessages(
error: unknown,
print: (message: string) => void = console.error,
): void {
const showStackTraces =
process.argv.includes("--show-stack-traces") ||
isCi() ||
getErrorWithCategory(error).category === ErrorCategory.OTHER;
const {
formattedErrorMessage,
showMoreInfoMessage,
postErrorStackTraceMessage,
} = getErrorMessages(error);
print(formattedErrorMessage);
print("");
if (showStackTraces) {
print(error instanceof Error ? `${error.stack}` : `${error}`);
if (postErrorStackTraceMessage !== undefined) {
print("");
print(postErrorStackTraceMessage);
}
} else if (showMoreInfoMessage !== undefined) {
print(showMoreInfoMessage);
}
}
function getErrorWithCategory(error: unknown): ErrorWithCategory {
if (HardhatError.isHardhatError(error)) {
if (error.pluginId === undefined) {
return {
category: ErrorCategory.HARDHAT,
categorizedError: error,
};
} else {
return {
category: ErrorCategory.PLUGIN,
categorizedError: error,
};
}
}
if (HardhatPluginError.isHardhatPluginError(error)) {
return {
category: ErrorCategory.COMMUNITY_PLUGIN,
categorizedError: error,
};
}
return {
category: ErrorCategory.OTHER,
categorizedError: error,
};
}
function getErrorMessages(error: unknown): ErrorMessages {
const { category, categorizedError } = getErrorWithCategory(error);
switch (category) {
case ErrorCategory.HARDHAT:
return {
formattedErrorMessage: `${chalk.red.bold(`Error ${categorizedError.errorCode}:`)} ${categorizedError.formattedMessage}`,
showMoreInfoMessage: `For more info go to ${HARDHAT_WEBSITE_URL}${categorizedError.errorCode} or run ${HARDHAT_NAME} with --show-stack-traces`,
};
case ErrorCategory.PLUGIN:
return {
formattedErrorMessage: `${chalk.red.bold(`Error ${categorizedError.errorCode} in plugin ${categorizedError.pluginId}:`)} ${categorizedError.formattedMessage}`,
showMoreInfoMessage: `For more info go to ${HARDHAT_WEBSITE_URL}${categorizedError.errorCode} or run ${HARDHAT_NAME} with --show-stack-traces`,
};
case ErrorCategory.COMMUNITY_PLUGIN:
return {
formattedErrorMessage: `${chalk.red.bold(`Error in community plugin ${categorizedError.pluginId}:`)} ${categorizedError.message}`,
showMoreInfoMessage: `For more info run ${HARDHAT_NAME} with --show-stack-traces`,
};
case ErrorCategory.OTHER:
return {
formattedErrorMessage: chalk.red.bold(`An unexpected error occurred:`),
postErrorStackTraceMessage: `If you think this is a bug in Hardhat, please report it here: ${HARDHAT_WEBSITE_URL}report-bug`,
};
}
}