web3.js icon indicating copy to clipboard operation
web3.js copied to clipboard

TypeError: Cannot use 'in' operator to search for 'originalError' in null

Open lvivo639 opened this issue 2 years ago • 3 comments

Expected behavior

Throwing error with message that my transaction/gas estimating was reverted

Actual behavior

\web3-errors\src\errors\contract_errors.ts:163 line throws next error:

 if ('originalError' in error.data) {
TypeError: Cannot use 'in' operator to search for 'originalError' in null

It can be thrown by web3.eth..estimateGas() or w3.eth.sendSignedTransaction().

My transaction was reverted and I just logged it in console (in library code) to understand what happened. Error object is:

{
  "code": -32000,
  "message": "execution reverted",
  "data": null
}

As soon as typeof null === 'object' is true web3-errors library throws an error above in this code:

if (typeof error.data === 'object') {
            let originalError;
            if ('originalError' in error.data) {

Error throwed in web3-errors\src\errors\contract_errors.ts:163

Steps to reproduce the behavior

I don't actually know why transaction is reverted. Code throws it randomly.

Logs

root\node_modules\web3-errors\src\errors\contract_errors.ts:163
                        if ('originalError' in error.data) {
                    ^
TypeError: Cannot use 'in' operator to search for 'originalError' in null
    at new Eip838ExecutionError (root\node_modules\web3-errors\src\errors\contract_errors.ts:163:21)
    at new ContractExecutionError (root\node_modules\web3-errors\src\errors\contract_errors.ts:225:3)
    at Function._isReverted (root\node_modules\web3-core\src\web3_request_manager.ts:432:48)
    at Web3RequestManager._processJsonRpcResponse (root\node_modules\web3-core\src\web3_request_manager.ts:361:35)
    at Web3RequestManager.<anonymous> (root\node_modules\web3-core\src\web3_request_manager.ts:206:16)
    at Generator.next (<anonymous>)
    at fulfilled (root\node_modules\web3-core\lib\commonjs\web3_request_manager.js:21:58)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)

Environment

npm - 9.8.1 node - 18.17.1 web3 - 4.1.1 OS - Windows 10

lvivo639 avatar Sep 16 '23 18:09 lvivo639

@lvivo639 Thanks for details, we will fix this.

jdevcs avatar Sep 18 '23 17:09 jdevcs

The in operator looks for the key in an object, but unfortunately null is also an object, which means typeof null === 'object'. Would like to fix this. can I open a new PR for this?

Alfxjx avatar Mar 08 '24 01:03 Alfxjx

@Alfxjx sure open PR for its fix, thanks

jdevcs avatar Mar 12 '24 09:03 jdevcs