Utils.js
1 /* global assert */ 2 3 function isException(error) { 4 let strError = error.toString(); 5 return strError.includes('VM Exception') || strError.includes('invalid opcode') || strError.includes('invalid JUMP'); 6 } 7 8 function ensureException(error) { 9 assert(isException(error), error.toString()); 10 } 11 12 const PREFIX = "VM Exception while processing transaction: "; 13 14 async function tryCatch(promise, message) { 15 try { 16 await promise; 17 throw null; 18 } 19 catch (error) { 20 assert(error, "Expected an error but did not get one"); 21 assert(error.message.startsWith(PREFIX + message), "Expected an error starting with '" + PREFIX + message + "' but got '" + error.message + "' instead"); 22 } 23 }; 24 25 module.exports = { 26 zeroAddress : '0x0000000000000000000000000000000000000000', 27 isException : isException, 28 ensureException : ensureException, 29 catchRevert : async function(promise) {await tryCatch(promise, "revert" );}, 30 catchOutOfGas : async function(promise) {await tryCatch(promise, "out of gas" );}, 31 catchInvalidJump : async function(promise) {await tryCatch(promise, "invalid JUMP" );}, 32 catchInvalidOpcode : async function(promise) {await tryCatch(promise, "invalid opcode" );}, 33 catchStackOverflow : async function(promise) {await tryCatch(promise, "stack overflow" );}, 34 catchStackUnderflow : async function(promise) {await tryCatch(promise, "stack underflow" );}, 35 catchStaticStateChange : async function(promise) {await tryCatch(promise, "static state change");}, 36 };