Skip to content

Commit

Permalink
Merge pull request #5198 from NomicFoundation/v-next-hardhat-errors
Browse files Browse the repository at this point in the history
[v-next] Port `HardhatError`
  • Loading branch information
alcuadrado committed May 13, 2024
2 parents 7433ddf + a913dec commit 76655ab
Show file tree
Hide file tree
Showing 15 changed files with 804 additions and 16 deletions.
9 changes: 8 additions & 1 deletion config-v-next/eslint.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,21 @@ function createConfig(configFilePath, packageEntryPoints = []) {
"@typescript-eslint/prefer-namespace-keyword": "error",
"@typescript-eslint/prefer-readonly": "error",
// "@typescript-eslint/prefer-readonly-parameter-types": "error", // TBD if we enable it
// This forces use to use native #private fields

"no-restricted-syntax": [
"error",
// This forces us to use native #private fields
{
selector:
':matches(PropertyDefinition, MethodDefinition[kind!="constructor"], TSParameterProperty)[accessibility="private"]',
message: "Use #private instead",
},
// We forbid `instanceof HardhatError` because it may not work well if the users has multiple versions of `@nomicfoundation/hardhat-errors` installed.
{
selector:
":matches(BinaryExpression[operator='instanceof']) > Identifier[name='HardhatError']",
message: "Use HardhatError.isHardhatError instead of instanceof",
},
],
"@typescript-eslint/restrict-plus-operands": "error",
"@typescript-eslint/restrict-template-expressions": [
Expand Down
74 changes: 64 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions v-next/hardhat-errors/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { createConfig } = require("../../config-v-next/eslint.cjs");

module.exports = createConfig(__filename, [
"src/index.ts",
"src/other-entry-point.ts",
]);
5 changes: 5 additions & 0 deletions v-next/hardhat-errors/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Node modules
/node_modules

# Compilation output
/dist
3 changes: 3 additions & 0 deletions v-next/hardhat-errors/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/node_modules
/dist
CHANGELOG.md
9 changes: 9 additions & 0 deletions v-next/hardhat-errors/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) 2024 Nomic Foundation

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9 changes: 9 additions & 0 deletions v-next/hardhat-errors/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Hardhat errors

This packages has the definition of the error classes used by Hardhat, and the list of possible errors.

This module exports:

1. The error class `HardhatError`, which has a static field `ERRORS`, with the different `ErrorDescriptors` that it accepts.
2. The interface `ErrorDescriptor`.
3. The assertion helper `assertHardhatInvariant`.
58 changes: 58 additions & 0 deletions v-next/hardhat-errors/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "@nomicfoundation/hardhat-errors",
"version": "3.0.0",
"description": "The different errors that Hardhat can throw",
"homepage": "https://github.com/nomicfoundation/hardhat/tree/v-next/v-next/hardhat-errors",
"repository": "github:nomicfoundation/hardhat",
"author": "Nomic Foundation",
"license": "MIT",
"type": "module",
"types": "dist/src/index.d.ts",
"exports": {
".": "./dist/src/index.js"
},
"keywords": [
"ethereum",
"smart-contracts",
"hardhat"
],
"scripts": {
"lint": "pnpm prettier --check && pnpm eslint",
"lint:fix": "pnpm prettier --write && pnpm eslint --fix",
"eslint": "eslint 'src/**/*.ts' 'test/**/*.ts'",
"prettier": "prettier \"**/*.{ts,js,md,json}\"",
"test": "glob --cmd=\"node --import tsx/esm --test\" 'test/**/*.ts'",
"test:github": "glob --cmd=\"node --import tsx/esm --test --test-reporter=@reporters/github --test-reporter-destination=stdout --test-reporter=spec --test-reporter-destination=stdout\" 'test/**/*.ts'",
"pretest": "pnpm build",
"build": "tsc --build .",
"prepublishOnly": "pnpm build",
"clean": "rimraf dist"
},
"files": [
"dist/src/",
"src/",
"CHANGELOG.md",
"LICENSE",
"README.md"
],
"devDependencies": {
"@nomicfoundation/hardhat-utils": "workspace:^3.0.0",
"@nomicfoundation/eslint-plugin-hardhat-internal-rules": "workspace:^",
"@nomicfoundation/eslint-plugin-slow-imports": "workspace:^",
"@reporters/github": "^1.7.0",
"@types/node": "^20.0.0",
"@typescript-eslint/eslint-plugin": "^7.7.1",
"@typescript-eslint/parser": "^7.7.1",
"eslint": "8.57.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-import": "2.29.1",
"eslint-plugin-no-only-tests": "3.1.0",
"expect-type": "^0.19.0",
"glob": "^10.3.12",
"prettier": "3.2.5",
"rimraf": "^5.0.5",
"tsx": "^4.7.1",
"typescript": "~5.4.0",
"typescript-eslint": "7.7.1"
}
}
80 changes: 80 additions & 0 deletions v-next/hardhat-errors/src/descriptors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* A description of a kind of error that Hardhat can throw.
*/
export interface ErrorDescriptor {
/**
* The error number, which should be unique.
*/
number: number;

/**
* The id of the plugin that throws this error.
*/
pluginId?: string;

/**
* A tempalte of the message of the error.
*
* This should be a short description. If possible, it should tell the user
* how to solve their problem.
*
* @see The `applyErrorMessageTemplate` function.
*/
messageTemplate: string;

/**
* `true` if this error should be reported
*/
shouldBeReported?: true;

/**
* The title to use on the website section explaining this error, which can
* use markdown.
*/
websiteTitle: string;

/**
* The description to use on the website section explaining this error, which
* can use markdown.
*/
websiteDescription: string;
}

export const ERROR_CATEGORIES: {
[categoryName: string]: {
min: number;
max: number;
websiteTitle: string;
};
} = {
GENERAL: { min: 1, max: 99, websiteTitle: "General errors" },
INTERNAL: { min: 100, max: 199, websiteTitle: "Internal Hardhat errors" },
};

export const ERRORS = {
GENERAL: {
NOT_INSIDE_PROJECT: {
number: 1,
messageTemplate: "You are not inside a Hardhat project.",
websiteTitle: "You are not inside a Hardhat project",
websiteDescription: `You are trying to run Hardhat outside of a Hardhat project.
You can learn how to use Hardhat by reading the [Getting Started guide](/hardhat-runner/docs/getting-started).`,
},
},
INTERNAL: {
ASSERTION_ERROR: {
number: 100,
messageTemplate: "An internal invariant was violated: %message%",
websiteTitle: "Invariant violation",
websiteDescription: `An internal invariant was violated. This is probably caused by a programming error in Hardhat or in one of the used plugins.
Please [report it](https://github.com/nomiclabs/hardhat/issues/new) to help us improve Hardhat.`,
shouldBeReported: true,
},
},
} satisfies {
[category in keyof typeof ERROR_CATEGORIES]: {
[name: string]: ErrorDescriptor;
};
};

0 comments on commit 76655ab

Please sign in to comment.