Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Truffle doesn't catch error and crashes mocha runner #2497

Open
1 task
mrwillis opened this issue Oct 25, 2019 · 13 comments
Open
1 task

Truffle doesn't catch error and crashes mocha runner #2497

mrwillis opened this issue Oct 25, 2019 · 13 comments

Comments

@mrwillis
Copy link
Contributor

mrwillis commented Oct 25, 2019


Issue

What the issue is, in broad strokes.

Code:

    it.only("should not allow double registration", async () => {
      const currentTime = await getCurrentTime(provider);
      const request: IMarketRequest = {
          ...
      };
      await marketRegistryWrapper.registerMarket(request, TX_DEFAULTS);
    try {
      const secondRegistration = marketRegistryWrapper.registerMarket(request, TX_DEFAULTS)
    } catch (e) {
     console.log(e)
     }
    });

Truffle test with chai cannot catch revert reason. The try catch block does not work and it crashes the entire runner

  Contract: MarketRegistry
    #registerMarket
      1) should not allow double registration
{ Error: VM Exception while processing transaction: revert MARKET_ALREADY_REGISTERED
    at /home/julian/betx/betx-contracts/node_modules/ethers/providers/web3-provider.js:74:35
    at /home/julian/betx/betx-contracts/node_modules/truffle/build/webpack:/packages/provider/wrapper.js:112:1
    at XMLHttpRequest.request.onreadystatechange (/home/julian/betx/betx-contracts/node_modules/truffle/build/webpack:/~/web3-providers-http/src/index.js:96:1)
    at XMLHttpRequestEventTarget.dispatchEvent (/home/julian/betx/betx-contracts/node_modules/truffle/build/webpack:/~/xhr2-cookies/dist/xml-http-request-event-target.js:34:1)
    at XMLHttpRequest._setReadyState (/home/julian/betx/betx-contracts/node_modules/truffle/build/webpack:/~/xhr2-cookies/dist/xml-http-request.js:208:1)
    at XMLHttpRequest._onHttpResponseEnd (/home/julian/betx/betx-contracts/node_modules/truffle/build/webpack:/~/xhr2-cookies/dist/xml-http-request.js:318:1)
    at IncomingMessage.<anonymous> (/home/julian/betx/betx-contracts/node_modules/truffle/build/webpack:/~/xhr2-cookies/dist/xml-http-request.js:289:47)
    at IncomingMessage.emit (events.js:187:15)
    at endReadableNT (_stream_readable.js:1081:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)
  code: -32000,
  data:
   { '0xc413ce66a13904b096e336bbe1574252dd09a5a0e6bea453befa1ca6c6d0c6ab':
      { error: 'revert',
        program_counter: 954,
        return:
         '0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000194d41524b45545f414c52454144595f5245474953544552454400000000000000',
        reason: 'MARKET_ALREADY_REGISTERED' },
     stack:
      'o: VM Exception while processing transaction: revert MARKET_ALREADY_REGISTERED\n    at Function.o.fromResults (/home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:10:89727)\n    at /home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:25:121953\n    at /home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:25:98498\n    at p (/home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:25:98156)\n    at /home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:25:98209\n    at t.default (/home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:37:544160)\n    at /home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:25:99122\n    at t.n.emit (/home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:61:16355)\n    at /home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:61:654328\n    at /home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:61:654351\n    at /home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:37:1093509\n    at /home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:19209\n    at i (/home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:34308)\n    at /home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:14717\n    at process._tickCallback (internal/process/next_tick.js:61:11)',
     name: 'o' } }
TruffleConfig {
  _deepCopy: [ 'compilers' ],
  _values:
   { truffle_directory: '/home/julian/betx/betx-contracts/node_modules/truffle',
     working_directory: '/home/julian/betx/betx-contracts',
     network: 'development',
     networks:
      { development: [Object],
        rinkeby: [Object],
        stage: [Object],
        production: [Object],
        'skale-development': [Object],
        'skale-rinkeby': [Object] },
     verboseRpc: false,
     gas: null,
     gasPrice: null,
     from: null,
     confirmations: 0,
     timeoutBlocks: 0,
     production: false,
     skipDryRun: false,
     build: null,
     resolver:
      TestResolver {
        resolver: [TestResolver],
        source: [TestSource],
        search_path: '/tmp/test-119925-20083-10twqa9.ls9g',
        seen: [],
        require_cache: {},
        cache_on: true },
     artifactor:
      Artifactor { destination: '/tmp/test-119925-20083-10twqa9.ls9g' },
     ethpm:
      { ipfs_host: 'ipfs.infura.io',
        ipfs_protocol: 'https',
        registry: '0x8011df4830b4f696cd81393997e5371b93338878',
        install_provider_uri: 'https://ropsten.infura.io/truffle' },
     compilers: { solc: [Object], vyper: {} },
     logger:
      Console {
        log: [Function: bound consoleCall],
        debug: [Function: bound consoleCall],
        info: [Function: bound consoleCall],
        dirxml: [Function: bound consoleCall],
        warn: [Function],
        error: [Function: bound consoleCall],
        dir: [Function: bound consoleCall],
        time: [Function: bound consoleCall],
        timeEnd: [Function: bound consoleCall],
        trace: [Function: bound consoleCall],
        assert: [Function: bound consoleCall],
        clear: [Function: bound consoleCall],
        count: [Function: bound consoleCall],
        countReset: [Function: bound countReset],
        group: [Function: bound consoleCall],
        groupCollapsed: [Function: bound consoleCall],
        groupEnd: [Function: bound consoleCall],
        table: [Function: bound consoleCall],
        Console: [Function: Console],
        markTimeline: [Function: markTimeline],
        profile: [Function: profile],
        profileEnd: [Function: profileEnd],
        timeline: [Function: timeline],
        timelineEnd: [Function: timelineEnd],
        timeStamp: [Function: timeStamp],
        context: [Function: context],
        [Symbol(counts)]: Map {},
        [Symbol(kColorMode)]: 'auto' },
     migrations_directory: '/home/julian/betx/betx-contracts/sidechain_migrations',
     contracts_build_directory: '/tmp/test-119925-20083-10twqa9.ls9g',
     build_directory: '/home/julian/betx/betx-contracts/build',
     contracts_directory: '/home/julian/betx/betx-contracts/contracts',
     migrations_file_extension_regexp: /^\.(js|es6?)$/,
     test_directory: '/home/julian/betx/betx-contracts/test',
     test_file_extension_regexp: /.*\.(js|ts|es|es6|jsx|sol)$/,
     example_project_directory:
      '/home/julian/betx/betx-contracts/node_modules/truffle/example' },
  truffle_directory: '/home/julian/betx/betx-contracts/node_modules/truffle',
  working_directory: '/home/julian/betx/betx-contracts',
  network: 'development',
  networks:
   { development:
      { host: '127.0.0.1',
        port: 8545,
        network_id: 1572017730959,
        gas: 6712390,
        from: '0x27f012c62149E79a5842f1ff779deDaF46CE6Bc5' },},
  verboseRpc: false,
  build: null,
  resolver:
   Resolver {
     options:
      TruffleConfig {
        _deepCopy: [Array],
        _values: [Object],
        truffle_directory: [Getter/Setter],
        working_directory: [Getter/Setter],
        network: [Getter/Setter],
        networks: [Getter/Setter],
        verboseRpc: [Getter/Setter],
        build: [Getter/Setter],
        resolver: [Getter/Setter],
        artifactor: [Getter/Setter],
        ethpm: [Getter/Setter],
        logger: [Getter/Setter],
        compilers: [Getter/Setter],
        build_directory: [Getter/Setter],
        contracts_directory: [Getter/Setter],
        contracts_build_directory: [Getter/Setter],
        migrations_directory: [Getter/Setter],
        migrations_file_extension_regexp: [Getter/Setter],
        test_directory: [Getter/Setter],
        test_file_extension_regexp: [Getter/Setter],
        example_project_directory: [Getter/Setter],
        network_id: [Getter/Setter],
        network_config: [Getter/Setter],
        from: [Getter/Setter],
        gas: [Getter/Setter],
        gasPrice: [Getter/Setter],
        provider: [Getter/Setter],
        confirmations: [Getter/Setter],
        production: [Getter/Setter],
        timeoutBlocks: [Getter/Setter],
        _: [Array],
        'show-events': false,
        showEvents: false },
     sources: [ [EPM], [NPM], GlobalNPM {}, [FS] ] },
  artifactor:
   Artifactor { destination: '/tmp/test-119925-20083-10twqa9.ls9g' },
  ethpm:
   { ipfs_host: 'ipfs.infura.io',
     ipfs_protocol: 'https',
     registry: '0x8011df4830b4f696cd81393997e5371b93338878',
     install_provider_uri: 'https://ropsten.infura.io/truffle' },
  logger:
   Console {
     log: [Function: bound consoleCall],
     debug: [Function: bound consoleCall],
     info: [Function: bound consoleCall],
     dirxml: [Function: bound consoleCall],
     warn: [Function],
     error: [Function: bound consoleCall],
     dir: [Function: bound consoleCall],
     time: [Function: bound consoleCall],
     timeEnd: [Function: bound consoleCall],
     trace: [Function: bound consoleCall],
     assert: [Function: bound consoleCall],
     clear: [Function: bound consoleCall],
     count: [Function: bound consoleCall],
     countReset: [Function: bound countReset],
     group: [Function: bound consoleCall],
     groupCollapsed: [Function: bound consoleCall],
     groupEnd: [Function: bound consoleCall],
     table: [Function: bound consoleCall],
     Console: [Function: Console],
     markTimeline: [Function: markTimeline],
     profile: [Function: profile],
     profileEnd: [Function: profileEnd],
     timeline: [Function: timeline],
     timelineEnd: [Function: timelineEnd],
     timeStamp: [Function: timeStamp],
     context: [Function: context],
     [Symbol(counts)]: Map {},
     [Symbol(kColorMode)]: 'auto' },
  compilers:
   { solc: { settings: [Object], version: '0.5.12' }, vyper: {} },
  build_directory: '/home/julian/betx/betx-contracts/build',
  contracts_directory: '/home/julian/betx/betx-contracts/contracts',
  contracts_build_directory: '/tmp/test-119925-20083-10twqa9.ls9g',
  migrations_directory: '/home/julian/betx/betx-contracts/sidechain_migrations',
  migrations_file_extension_regexp: /^\.(js|es6?)$/,
  test_directory: '/home/julian/betx/betx-contracts/test',
  test_file_extension_regexp: /.*\.(js|ts|es|es6|jsx|sol)$/,
  example_project_directory:
   '/home/julian/betx/betx-contracts/node_modules/truffle/example',
  network_id: 1572017730959,
  network_config:
   { gas: 6712390,
     gasPrice: 20000000000,
     from: '0x27f012c62149E79a5842f1ff779deDaF46CE6Bc5',
     host: '127.0.0.1',
     port: 8545,
     network_id: 1572017730959 },
  from: '0x27f012c62149E79a5842f1ff779deDaF46CE6Bc5',
  gas: 6712390,
  gasPrice: 20000000000,
  provider:
   HttpProvider {
     host: 'http://127.0.0.1:8545',
     httpAgent:
      Agent {
        _events: [Object],
        _eventsCount: 1,
        _maxListeners: undefined,
        defaultPort: 80,
        protocol: 'http:',
        options: [Object],
        requests: {},
        sockets: {},
        freeSockets: {},
        keepAliveMsecs: 1000,
        keepAlive: false,
        maxSockets: Infinity,
        maxFreeSockets: 256 },
     timeout: 0,
     headers: undefined,
     connected: false,
     send: [Function],
     _alreadyWrapped: true },
  confirmations: undefined,
  production: undefined,
  timeoutBlocks: undefined,
  _: [ 'dist/test/market_registry.js' ],
  'show-events': false,
  showEvents: false,
  test_files: [ 'dist/test/market_registry.js' ] }
/home/julian/betx/betx-contracts/node_modules/mocha/lib/runner.js:726
  err.uncaught = true;
               ^

TypeError: Cannot create property 'uncaught' on string 'abort({"code":-32000,"data":{"0xc413ce66a13904b096e336bbe1574252dd09a5a0e6bea453befa1ca6c6d0c6ab":{"error":"revert","program_counter":954,"return":"0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000194d41524b45545f414c52454144595f5245474953544552454400000000000000","reason":"MARKET_ALREADY_REGISTERED"},"stack":"o: VM Exception while processing transaction: revert MARKET_ALREADY_REGISTERED\n    at Function.o.fromResults (/home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:10:89727)\n    at /home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:25:121953\n    at /home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:25:98498\n    at p (/home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:25:98156)\n    at /home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:25:98209\n    at t.default (/home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:37:544160)\n    at /home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:25:99122\n    at t.n.emit (/home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:61:16355)\n    at /home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:61:654328\n    at /home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:61:654351\n    at /home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:37:1093509\n    at /home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:19209\n    at i (/home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:34308)\n    at /home/julian/.nvm/versions/node/v10.5.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:14717\n    at process._tickCallback (internal/process/next_tick.js:61:11)","name":"o"}}). Build with -s ASSERTIONS=1 for more info.'
    at Runner.uncaught (/home/julian/betx/betx-contracts/node_modules/mocha/lib/runner.js:726:16)
    at process.uncaught (/home/julian/betx/betx-contracts/node_modules/mocha/lib/runner.js:839:10)
    at process.emit (events.js:182:13)
    at process.emit (/home/julian/betx/betx-contracts/node_modules/truffle/build/webpack:/~/source-map-support/source-map-support.js:465:1)
    at process.emit (/home/julian/betx/betx-contracts/node_modules/ganache-core/node_modules/source-map-support/source-map-support.js:465:21)
    at process._fatalException (internal/bootstrap/node.js:441:27)

Steps to Reproduce

Please provide the shortest amount of steps to reproduce your issue.

Expected Behavior

What you expected to happen.

The test passes

Actual Results

What actually happened. Please give examples and support it with screenshots, copied output or error messages.

Output from ganache-cli:

eth_getTransactionByHash
eth_sendTransaction
evm_revert

  Transaction: 0xc413ce66a13904b096e336bbe1574252dd09a5a0e6bea453befa1ca6c6d0c6ab
  Gas usage: 37283
  Block Number: 74
  Block Time: Fri Oct 25 2019 11:36:09 GMT-0400 (Eastern Daylight Time)
  Runtime Error: revert
  Revert reason: MARKET_ALREADY_REGISTERED

Reverting to snapshot #4

Related:

mochajs/mocha#3320

The issue seems to be that this thing is throwing a string, an not an object.

Environment

  • Operating System: ubuntu 18.04

  • Ethereum client: Ganache CLI v6.7.0-beta.0 (ganache-core: 2.8.0-beta.0)

  • Truffle version (truffle version):
    Truffle v5.0.41 (core: 5.0.41)
    Solidity v0.5.8 (solc-js)
    Node v10.5.0
    Web3.js v1.2.1

  • node version (node --version): v10.5.0

  • npm version (npm --version): 6.12.0

@mrwillis mrwillis changed the title Truffle doesn't catch revert reason in error and bubbles up Truffle doesn't catch error and crashes mocha runner Oct 25, 2019
@gnidan
Copy link
Contributor

gnidan commented Oct 30, 2019

Thanks for bringing this up! Sounds like we need an extra try/catch somewhere. Will get this on our backlog for investigation, or anyone feel free to take a look and open a PR! 🙇

@gnidan gnidan added bug error better issue indicates improvement to error messaging help wanted truffle test labels Oct 30, 2019
@rogerioyuuki
Copy link

I'm also having this problem and what I noticed is the weird string being thrown instead of an Error object. The string is abort(...something...). Build with -s ASSERTIONS=1 for more info., which I think comes from here:
https://github.com/emscripten-core/emscripten/blob/8d265e61d4313246b0682c6298b42c640aaaf046/src/preamble.js#L722

And this crashes the test because mocha doesn't support strings being thrown (mochajs/mocha#3321)

@eggplantzzz
Copy link
Contributor

Do you have a specific example that I could use to reproduce this error? If I just make a Solidity function that reverts it doesn't crash the test runner.

@digulla
Copy link

digulla commented Nov 27, 2019

Workaround: Add

if (typeof(err) === 'string') {
  err = Error(err);
}

after the line

Runner.prototype.uncaught = function(err) {

of ./node_modules/truffle/node_modules/mocha/lib/runner.js (around line 730).

@digulla
Copy link

digulla commented Nov 28, 2019

I almost always get this when moving common code into an async helper method and a contract method fails there. Example:

async function foo() {
    assert.fail("always");
}

it("test mocha", async () => { await foo(); }

@fainashalts
Copy link
Contributor

@mrwillis @digulla can you provide specific reproduction steps for this so we can dig in and see what's going on? Thanks so much!

@gbenroscience
Copy link

I am using:
Truffle v5.1.30 (core: 5.1.30)
Solidity - 0.6.10 (solc-js)
Node v12.18.0
Web3.js v1.2.1

And I can confirm that transaction reverts in Solidity code crashes and exits the truffle environment

@ashajjar
Copy link

ashajjar commented Feb 2, 2022

@fainashalts Here is an another example:

  it("should not allow owner to participate", async function () {
    try {
      await instance.participate("test", {
        from: accounts[0],
        value: 1
      });
      assert(false);
    } catch (error) {
      assert.ok(true);
    }
  });

this is how it ends up 😅

  1) Contract: TestContact
       should not allow owner to participate:
     Uncaught TypeError: Cannot use 'in' operator to search for 'result' in 0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000086973206f776e6572000000000000000000000000000000000000000000000000
      at Object._extract (/home/ahmed/.nvm/versions/node/v16.13.2/lib/node_modules/truffle/build/webpack:/packages/contract/lib/reason.js:31:1)
      at /home/ahmed/.nvm/versions/node/v16.13.2/lib/node_modules/truffle/build/webpack:/packages/contract/lib/reason.js:79:1
      at /home/ahmed/.nvm/versions/node/v16.13.2/lib/node_modules/truffle/build/webpack:/packages/provider/wrapper.js:107:1
      at XMLHttpRequest.request.onreadystatechange (/home/ahmed/.nvm/versions/node/v16.13.2/lib/node_modules/truffle/build/webpack:/node_modules/web3/node_modules/web3-providers-http/lib/index.js:98:1)
      at XMLHttpRequestEventTarget.dispatchEvent (/home/ahmed/.nvm/versions/node/v16.13.2/lib/node_modules/truffle/build/webpack:/node_modules/xhr2-cookies/dist/xml-http-request-event-target.js:34:1)
      at XMLHttpRequest.exports.modules.996763.XMLHttpRequest._setReadyState (/home/ahmed/.nvm/versions/node/v16.13.2/lib/node_modules/truffle/build/webpack:/node_modules/xhr2-cookies/dist/xml-http-request.js:208:1)
      at XMLHttpRequest.exports.modules.996763.XMLHttpRequest._onHttpResponseEnd (/home/ahmed/.nvm/versions/node/v16.13.2/lib/node_modules/truffle/build/webpack:/node_modules/xhr2-cookies/dist/xml-http-request.js:318:1)
      at IncomingMessage.<anonymous> (/home/ahmed/.nvm/versions/node/v16.13.2/lib/node_modules/truffle/build/webpack:/node_modules/xhr2-cookies/dist/xml-http-request.js:289:47)
      at IncomingMessage.emit (node:events:402:35)
      at endReadableNT (node:internal/streams/readable:1343:12)
      at processTicksAndRejections (node:internal/process/task_queues:83:21)


@eggplantzzz
Copy link
Contributor

Hey @ashajjar this looks kind of similar to a bug we had in a recent version or two of Truffle. Can you upgrade to the latest Truffle (v5.4.31) and see if you still get the error?

@ashajjar
Copy link

ashajjar commented Feb 2, 2022

I was only one patch behind 😅 v5.4.30 ... now it works on v5.4.31 🎉

I took me 24 hours of scratching my head!!

@eggplantzzz
Copy link
Contributor

Sorry to cause you trouble! That was the release that included the next major release of Ganache (v7) and we had a bug or two to iron out. Glad your problem was resolved!

@ashajjar
Copy link

ashajjar commented Feb 5, 2022

Thanks a lot :)

@cds-amal
Copy link
Member

cds-amal commented Feb 6, 2022

Is this still an issue?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

9 participants