From 6088ac53cf539892b2c78cccf0940a876226b2f3 Mon Sep 17 00:00:00 2001 From: Zoey Date: Wed, 11 Oct 2023 06:21:19 -0400 Subject: [PATCH] handle error thrown by status task (#541) Use a whitelist to determine whether a code `IgnitionError` should be rethrown as a Hardhat plugin error. This list can be added to as more Ignition errors are identified as being better displayed as HH errors. We intend to extend the HH error code mechanism to give Ignition even more control over the display of errors in HH. This change only adds this rethrow for status and the unrecognised deployment entry currently. Fixes #527. --- packages/hardhat-plugin/src/index.ts | 18 ++++++++++++++---- .../src/utils/shouldBeHardhatPluginError.ts | 12 ++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 packages/hardhat-plugin/src/utils/shouldBeHardhatPluginError.ts diff --git a/packages/hardhat-plugin/src/index.ts b/packages/hardhat-plugin/src/index.ts index dd2d0f6f94..9fe405fc73 100644 --- a/packages/hardhat-plugin/src/index.ts +++ b/packages/hardhat-plugin/src/index.ts @@ -1,16 +1,17 @@ -import type { +import { DeploymentParameters, + IgnitionError, StatusResult, } from "@nomicfoundation/ignition-core"; - import "@nomicfoundation/hardhat-ethers"; import chalk from "chalk"; import { readdirSync } from "fs-extra"; import { extendConfig, extendEnvironment, scope } from "hardhat/config"; -import { lazyObject } from "hardhat/plugins"; +import { NomicLabsHardhatPluginError, lazyObject } from "hardhat/plugins"; import path from "path"; import "./type-extensions"; +import { shouldBeHardhatPluginError } from "./utils/shouldBeHardhatPluginError"; /* ignition config defaults */ const IGNITION_DIR = "ignition"; @@ -247,7 +248,16 @@ ignitionScope id ); - const statusResult = await status(deploymentDir); + let statusResult: StatusResult; + try { + statusResult = await status(deploymentDir); + } catch (e) { + if (e instanceof IgnitionError && shouldBeHardhatPluginError(e)) { + throw new NomicLabsHardhatPluginError("hardhat-ignition", e.message); + } + + throw e; + } if (statusResult.started.length > 0) { console.log(""); diff --git a/packages/hardhat-plugin/src/utils/shouldBeHardhatPluginError.ts b/packages/hardhat-plugin/src/utils/shouldBeHardhatPluginError.ts new file mode 100644 index 0000000000..9522f5572e --- /dev/null +++ b/packages/hardhat-plugin/src/utils/shouldBeHardhatPluginError.ts @@ -0,0 +1,12 @@ +import { IgnitionError } from "@nomicfoundation/ignition-core"; + +/* + This is a whitelist of error codes that should be rethrown as NomicLabsHardhatPluginError. + */ +const whitelist = ["800"]; + +export function shouldBeHardhatPluginError(error: IgnitionError): boolean { + const code = error.message.match(/IGN([0-9]+):/)![1]; + + return whitelist.includes(code); +}