From 584eb5e4ff875a9d8dca71e4492076d58f288cc3 Mon Sep 17 00:00:00 2001 From: Denis Davidyuk Date: Thu, 18 Mar 2021 13:00:34 +0300 Subject: [PATCH] Improve handling of call error --- es/ae/contract.js | 12 ++++++------ test/integration/contract.js | 10 +++++++++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/es/ae/contract.js b/es/ae/contract.js index 06583a7e37..83b0062cbd 100644 --- a/es/ae/contract.js +++ b/es/ae/contract.js @@ -37,6 +37,7 @@ import NodePool from '../node-pool' import { AMOUNT, DEPOSIT, DRY_RUN_ACCOUNT, GAS, MIN_GAS_PRICE } from '../tx/builder/schema' import { decode, produceNameId } from '../tx/builder/helpers' import TxObject from '../tx/tx-object' +import { decodeBase64Check } from '../utils/crypto' async function sendAndProcess (tx, options) { const txData = await this.send(tx, options) @@ -66,17 +67,16 @@ async function sendAndProcess (tx, options) { * @return {Promise} */ async function _handleCallError (result, rawTx) { - const error = Buffer.from(result.returnValue).toString() - const decodedError = isBase64(error.slice(3)) - ? Buffer.from(error.slice(3), 'base64').toString() + let error = Buffer.from(result.returnValue).toString() + error = isBase64(error.slice(3)) + ? decodeBase64Check(error.slice(3)).toString() : await this.contractDecodeDataAPI('string', error) throw Object.assign( - new Error(`Invocation failed: ${error}. Decoded: ${decodedError}`), { + new Error(`Invocation failed${error ? `: "${error}"` : ''}`), { ...result, tx: TxObject({ tx: rawTx }), error, - rawTx, - decodedError + rawTx } ) } diff --git a/test/integration/contract.js b/test/integration/contract.js index eb0b836c27..572b218740 100644 --- a/test/integration/contract.js +++ b/test/integration/contract.js @@ -32,6 +32,9 @@ contract Identity = const errorContract = ` contract Identity = payable stateful entrypoint main(x : address) = Chain.spend(x, 1000000000) + + payable stateful entrypoint foo() = + require(false, "CustomErrorMessage") ` const stateContract = ` @@ -371,7 +374,12 @@ describe('Contract', function () { try { await deployed.call('main', [await contract.address()]) } catch (e) { - e.message.indexOf('Invocation failed').should.not.be.equal(-1) + e.message.should.be.equal('Invocation failed') + } + try { + await deployed.call('foo') + } catch (e) { + e.message.should.be.equal('Invocation failed: "ICustomErrorMessage"') } })