【问题标题】:Catching error from assert in promise chain从承诺链中的断言中捕获错误
【发布时间】:2018-03-01 20:17:18
【问题描述】:

您好,我正在尝试捕捉承诺链中的错误,例如:

it("Exception is thrown for Invalid Candidate",function(){
      return Election.deployed().then(function(instance){
           electionInstance = instance;
           candidateId = 99;
           return electionInstance.vote(candidateId,{from:accounts[1]});
      }).then(assert.fail).catch(function(error){
           assert(error.message.indexOf('revert') => 0,"error message must contain revert");
           return electionInstance.candidates(1);
      }).then(function(candidate1){
           var voteCount = candidate1[0];
           assert.equal(voteCount,1,"candidate1 did not recieve any votes");
           return electionInstance.candidates(2);
      }).then(function(candidate2){
           var voteCount = candidate2[0];
           assert.equal(voteCount,0,"Candidate2 didnot recieve any votes");
      });
 });

但我在 error.message 附近收到语法错误。 在 chaijs 文档中找不到任何有用的东西。 我的方法错了吗?我该怎么做? 错误如下

/home/chance/Ethereum_Work/VotingApplication/test/election.js:48
           assert(error.message.indexOf('revert') => 0,"error message must contain revert");
                       ^


SyntaxError: Unexpected token .
at new Script (vm.js:51:7)
at createScript (vm.js:138:10)
at Object.runInThisContext (vm.js:199:10)
at Module._compile (module.js:624:28)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Module.require (module.js:604:17)
at require (internal/module.js:11:18)
at /home/linuxbrew/.linuxbrew/lib/node_modules/truffle/node_modules/mocha/lib/mocha.js:231:27
at Array.forEach (<anonymous>)
at Mocha.loadFiles (/home/linuxbrew/.linuxbrew/lib/node_modules/truffle/node_modules/mocha/lib/mocha.js:228:14)
at Mocha.run (/home/linuxbrew/.linuxbrew/lib/node_modules/truffle/node_modules/mocha/lib/mocha.js:514:10)
at /home/linuxbrew/.linuxbrew/lib/node_modules/truffle/build/webpack:/~/truffle-core/lib/test.js:125:1
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:160:7)

【问题讨论】:

  • “但我在 error.message 附近收到语法错误” 真的吗?也许是因为语法确实无效。
  • 发布错误信息。如果错误没有属性message,它会在尝试调用indexOf
  • 我们很乐意帮助您理解语法错误,但您必须与我们分享语法错误的确切内容,它发生在哪一行代码上,然后我们可能需要查看语法错误中涉及的变量包含什么。
  • someAction() 拒绝什么?我假设一些具有消息属性的对象?你的语法错误说明了什么?

标签: javascript node.js exception-handling


【解决方案1】:

那么如果你把下面的而不是消息,会发生什么?

error.toString().indexOf('revert') >= 0

这以正确的输出成功通过了测试。

这里是来源:

https://expertcodeblog.wordpress.com/2018/01/15/typescript-how-to-resolve-error-indexof-is-not-a-function/

由 MARCO BARBERO 于 2018 年 1 月 15 日发布

有时在 TypeScript 上,当你调用 indexOf() 方法时,你会得到错误“indexOf is not a function”。当变量中包含的数据由没有其他字符的数字表示时会发生这种情况,即使变量已被声明为字符串。

要解决错误,您应该使用 toString() 方法:

myStringVar.toString().indexOf('mysubstring');

【讨论】:

    【解决方案2】:

    这段代码:

    error.message.indexOf('revert') => 0
    

    是一个无效的 Javascript 表达式,因为 =&gt; 用于表示粗箭头回调的声明,这不是放置回调的适当位置,因此会导致错误。

    你也许是这个意思?

    error.message.indexOf('revert') === 0
    

    或者这个:

    error.message.indexOf('revert') >= 0
    

    【讨论】:

    • 我猜他的意思是&gt;=
    • @Bergi - 好猜测。我补充说这是一种可能性。
    • 是的,昏昏欲睡的编码是毒药。 >= 谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 2018-06-06
    • 1970-01-01
    • 2021-04-17
    相关资源
    最近更新 更多