From d3b32ebf86359a1f4a360c2a0d3df169a97414cd Mon Sep 17 00:00:00 2001 From: Eyal Chojnowski Date: Tue, 28 Nov 2023 14:42:57 +0100 Subject: [PATCH] :zap: Improve error handling for Rust contracts --- src/contract/HandlerBasedContract.ts | 13 ++++++++++--- src/core/modules/impl/handler/WasmHandlerApi.ts | 3 ++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/contract/HandlerBasedContract.ts b/src/contract/HandlerBasedContract.ts index ca219d7b..5a7fb1fb 100644 --- a/src/contract/HandlerBasedContract.ts +++ b/src/contract/HandlerBasedContract.ts @@ -3,6 +3,7 @@ import { SortKeyCacheResult } from '../cache/SortKeyCache'; import { ContractCallRecord, InteractionCall } from '../core/ContractCallRecord'; import { ExecutionContext } from '../core/ExecutionContext'; import { + ContractError, ContractInteraction, HandlerApi, InteractionData, @@ -461,7 +462,9 @@ export class HandlerBasedContract implements Contract { ? await arweave.wallets.ownerToAddress(owner) : await this._signature.getAddress(); const handlerResult = await this.callContract(input, 'write', caller, undefined, tags, transfer, strict, vrf); - if (handlerResult.type !== 'ok') { + if (handlerResult.type === 'error') { + throw new ContractError(handlerResult.error); + } else if (handlerResult.type !== 'ok') { throw Error('Cannot create interaction: ' + JSON.stringify(handlerResult.error || handlerResult.errorMessage)); } } @@ -1034,8 +1037,12 @@ export class HandlerBasedContract implements Contract { false ); - if (strict && handlerResult.type !== 'ok') { - throw Error('Cannot create interaction: ' + JSON.stringify(handlerResult.error || handlerResult.errorMessage)); + if (strict) { + if (handlerResult.type === 'error') { + throw new ContractError(handlerResult.error); + } else if (handlerResult.type !== 'ok') { + throw Error('Cannot create interaction: ' + JSON.stringify(handlerResult.error || handlerResult.errorMessage)); + } } const callStack: ContractCallRecord = this.getCallStack(); const innerWrites = this._innerWritesEvaluator.eval(callStack); diff --git a/src/core/modules/impl/handler/WasmHandlerApi.ts b/src/core/modules/impl/handler/WasmHandlerApi.ts index bd7c16d2..2e3ea885 100644 --- a/src/core/modules/impl/handler/WasmHandlerApi.ts +++ b/src/core/modules/impl/handler/WasmHandlerApi.ts @@ -120,8 +120,9 @@ export class WasmHandlerApi extends AbstractContractHandler { if (typeof handleResult.Err === 'string' || handleResult.Err instanceof String) { errorKey = handleResult.Err; } else if ('kind' in handleResult.Err) { + throw new ContractError(handleResult.Err); errorKey = handleResult.Err.kind; - errorArgs = 'data' in handleResult.Err ? ' ' + handleResult.Err.data : ''; + errorArgs = 'data' in handleResult.Err ? ' ' + JSON.stringify(handleResult.Err.data, null, 2) : ''; } else { errorKey = Object.keys(handleResult.Err)[0]; errorArgs = ' ' + handleResult.Err[errorKey];