ethers-decode-error icon indicating copy to clipboard operation
ethers-decode-error copied to clipboard

Fix "Cannot read properties of null (reading 'data')" issue when decoding error from ethers.js

Open randydu opened this issue 11 months ago • 2 comments

The errorData could be null when the error is thrown from hardhat / ethers.js, hence a "Cannot read properties of null (reading 'data')" error will occur when setting returnData at line 67.

Example error causing the issue:

{
code: 'UNKNOWN_ERROR',
error: {
    code: -32000,
    message: "Nonce too high. Expected nonce to be 0 but got 33. Note that transactions can't be queued when automining.",
    data: {
      message: "Nonce too high. Expected nonce to be 0 but got 33. Note that transactions can't be queued when automining.",
      data: null <===  HERE is a null data!!!!
    }
  },
payload: {
    method: 'eth_sendRawTransaction',
    params: [
      '0x02f8b2827a6921843b9aca00843e9a078083015644944abeaca4b05d8fa4ced09d26ad28ea298e8afac880b844a9059cbb0000000000000000000000009965507d1a55bcc2695c58ba16fb37d819b0a4dc0000000000000000000000000000000000000000000000000000000000000064c001a03c032ec0cd43679f7400e1c90a8c9c07fe3d87ed4e0bf78ec5097046cf16d1eaa03cc5e482a939684ef216bfdeb72cb15fd36558a9df53de3745e3a27eb60f7869'
    ],
    id: 28,
    jsonrpc: '2.0'
  },
  shortMessage: 'could not coalesce error'
}

randydu avatar Mar 20 '25 23:03 randydu

I have not double checked if my fix really works for the error, it seems the error is not raised from line 67.

Here is the stack trace when the error is thrown:

stacktrace": [
          "TypeError: Cannot read properties of null (reading 'data')",
          "    at e.getDataFromError (file:///work/blocpal/tokendev/api/node_modules/ethers-decode-error/dist/index.mjs:1:5439)",
          "    at file:///work/blocpal/tokendev/api/node_modules/ethers-decode-error/dist/index.mjs:1:5647",

randydu avatar Mar 20 '25 23:03 randydu

Thanks @randydu for creating this pull request!😊 Just want to check if I understood the issue correctly, the issue you’re facing happens when the nonce is incorrectly passed to Ethers when sending a transaction and the data field in the error object thrown by Ethers is null but the library is attempts to from it. Did I understand the issue correctly?

superical avatar Mar 22 '25 01:03 superical