customError.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // DEPRECATED: replace with hardhat-toolbox chai matchers.
  2. const { expect } = require('chai');
  3. /** Revert handler that supports custom errors. */
  4. async function expectRevertCustomError(promise, expectedErrorName, args) {
  5. if (!Array.isArray(args)) {
  6. expect.fail('Expected 3rd array parameter for error arguments');
  7. }
  8. await promise.then(
  9. () => expect.fail("Expected promise to throw but it didn't"),
  10. ({ message }) => {
  11. // The revert message for custom errors looks like:
  12. // VM Exception while processing transaction:
  13. // reverted with custom error 'InvalidAccountNonce("0x70997970C51812dc3A010C7d01b50e0d17dc79C8", 0)'
  14. // Attempt to parse as a custom error
  15. const match = message.match(/custom error '(?<name>\w+)\((?<args>.*)\)'/);
  16. if (!match) {
  17. expect.fail(`Could not parse as custom error. ${message}`);
  18. }
  19. // Extract the error name and parameters
  20. const errorName = match.groups.name;
  21. const argMatches = [...match.groups.args.matchAll(/-?\w+/g)];
  22. // Assert error name
  23. expect(errorName).to.be.equal(
  24. expectedErrorName,
  25. `Unexpected custom error name (with found args: [${argMatches.map(([a]) => a)}])`,
  26. );
  27. // Coerce to string for comparison since `arg` can be either a number or hex.
  28. const sanitizedExpected = args.map(arg => arg.toString().toLowerCase());
  29. const sanitizedActual = argMatches.map(([arg]) => arg.toString().toLowerCase());
  30. // Assert argument equality
  31. expect(sanitizedActual).to.have.members(sanitizedExpected, `Unexpected ${errorName} arguments`);
  32. },
  33. );
  34. }
  35. module.exports = {
  36. expectRevertCustomError,
  37. };