Skip to content

Commit

Permalink
Merge pull request #3470 from NomicFoundation/return-data-custom-error
Browse files Browse the repository at this point in the history
Show return data of unrecognized custom errors
  • Loading branch information
fvictorio authored Dec 27, 2022
2 parents 6bbc14e + 23a594a commit 727740f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/warm-radios-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"hardhat": patch
---

The full return data of unrecognized custom errors is now shown in error messages
Original file line number Diff line number Diff line change
Expand Up @@ -519,12 +519,8 @@ export class ErrorInferrer {
return;
}

let errorMessage = "reverted with an unrecognized custom error";

const selector = returnData.getSelector();
if (selector !== undefined) {
errorMessage += ` with selector ${selector}`;
}
const rawReturnData = returnData.value.toString("hex");
let errorMessage = `reverted with an unrecognized custom error (return data: 0x${rawReturnData})`;

for (const customError of trace.bytecode.contract.customErrors) {
if (returnData.matchesSelector(customError.selector)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,16 +271,9 @@ function getMessageFromLastStackTraceEntry(
}

if (!stackTraceEntry.message.isEmpty()) {
const selector = stackTraceEntry.message.getSelector();
const returnData = stackTraceEntry.message.value.toString("hex");

if (selector !== undefined) {
return `VM Exception while processing transaction: reverted with an unrecognized custom error with selector ${selector}`;
}

// this should only happen when the transaction has return data but it
// has less than 4 bytes, which should be a very uncommon scenario
const rawReturnData = stackTraceEntry.message.value.toString("hex");
return `VM Exception while processing transaction: reverted with return data ${rawReturnData}`;
return `VM Exception while processing transaction: reverted with an unrecognized custom error (return data: 0x${returnData})`;
}

if (stackTraceEntry.isInvalidOpcodeError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ describe("Eth module", function () {
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
data: "0x6080604052348015600f57600080fd5b506040517ffbd8bc9c00000000000000000000000000000000000000000000000000000000815260040160405180910390fdfe",
},
"VM Exception while processing transaction: reverted with an unrecognized custom error with selector fbd8bc9c"
"VM Exception while processing transaction: reverted with an unrecognized custom error (return data: 0xfbd8bc9c)"
);

// This deploys a contract that divides by zero in its contstructor:
Expand Down

0 comments on commit 727740f

Please sign in to comment.