Skip to content

Commit

Permalink
⚡ Improve error handling for Rust contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
noomly committed Nov 28, 2023
1 parent b92010f commit d3b32eb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/contract/HandlerBasedContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -461,7 +462,9 @@ export class HandlerBasedContract<State> implements Contract<State> {
? 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));
}
}
Expand Down Expand Up @@ -1034,8 +1037,12 @@ export class HandlerBasedContract<State> implements Contract<State> {
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);
Expand Down
3 changes: 2 additions & 1 deletion src/core/modules/impl/handler/WasmHandlerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ export class WasmHandlerApi<State> extends AbstractContractHandler<State> {
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];
Expand Down

0 comments on commit d3b32eb

Please sign in to comment.