【发布时间】: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