Skip to content

Commit

Permalink
Improve handling of call error
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Mar 18, 2021
1 parent fe7a856 commit 584eb5e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
12 changes: 6 additions & 6 deletions es/ae/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -66,17 +67,16 @@ async function sendAndProcess (tx, options) {
* @return {Promise<void>}
*/
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
}
)
}
Expand Down
10 changes: 9 additions & 1 deletion test/integration/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand Down Expand Up @@ -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"')
}
})

Expand Down

0 comments on commit 584eb5e

Please sign in to comment.