Skip to content

Commit

Permalink
handle error thrown by status task (#541)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zoeyTM authored Oct 11, 2023
1 parent 3384565 commit 6088ac5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
18 changes: 14 additions & 4 deletions packages/hardhat-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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("");
Expand Down
12 changes: 12 additions & 0 deletions packages/hardhat-plugin/src/utils/shouldBeHardhatPluginError.ts
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 6088ac5

Please sign in to comment.