From 5190f7a20fb35382756d86cc67d3fab5c2d541ff Mon Sep 17 00:00:00 2001 From: Arcticae Date: Mon, 20 Jun 2022 16:21:07 +0200 Subject: [PATCH] fix(provider): allow the user to handle the contract errors --- src/contract/default.ts | 8 ++++++-- src/provider/default.ts | 8 +++++--- src/provider/utils.ts | 6 ++++++ src/types/contract.ts | 3 ++- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/contract/default.ts b/src/contract/default.ts index e079e7501..9f22372ff 100644 --- a/src/contract/default.ts +++ b/src/contract/default.ts @@ -17,6 +17,7 @@ import { Overrides, ParsedStruct, Result, + ResultAccessor, StructAbi, } from '../types'; import { BigNumberish, toBN, toFelt } from '../utils/number'; @@ -523,7 +524,10 @@ export class Contract implements ContractInterface { * @param response - response from the method * @return - parsed response corresponding to the abi */ - protected parseResponse(method: string, response: string[]): Result { + protected parseResponse(method: string, response?: string[]): Result { + if (!response) { + return undefined; + } const { outputs } = this.abi.find((abi) => abi.name === method) as FunctionAbi; const responseIterator = response.flat()[Symbol.iterator](); const resultObject = outputs.flat().reduce((acc, output) => { @@ -537,7 +541,7 @@ export class Contract implements ContractInterface { acc.push(value); acc[key] = value; return acc; - }, [] as Result); + }, [] as ResultAccessor); } public invoke( diff --git a/src/provider/default.ts b/src/provider/default.ts index 64c4f1f8a..8f2cff421 100644 --- a/src/provider/default.ts +++ b/src/provider/default.ts @@ -25,7 +25,7 @@ import { parse, stringify } from '../utils/json'; import { BigNumberish, bigNumberishArrayToDecimalStringArray, toBN, toHex } from '../utils/number'; import { compressProgram, randomAddress } from '../utils/stark'; import { ProviderInterface } from './interface'; -import { BlockIdentifier, getFormattedBlockIdentifier } from './utils'; +import { BlockIdentifier, GatewayError, getFormattedBlockIdentifier } from './utils'; type NetworkName = 'mainnet-alpha' | 'goerli-alpha'; @@ -159,9 +159,11 @@ export class Provider implements ProviderInterface { body: stringify(request), headers, }) - .then((res) => { + .then(async (res) => { if (res.status >= 400) { - throw Error(res.statusText); + // This will allow user to handle contract errors + const responseBody = parse(await res.text()); + throw new GatewayError(responseBody.message, responseBody.code); } return res.text(); }) diff --git a/src/provider/utils.ts b/src/provider/utils.ts index e1d4e4434..a954d943e 100644 --- a/src/provider/utils.ts +++ b/src/provider/utils.ts @@ -82,3 +82,9 @@ export function getFormattedBlockIdentifier(blockIdentifier: BlockIdentifier = n } return `blockHash=${toHex(toBN(blockIdentifierObject.data))}`; } + +export class GatewayError extends Error { + constructor(message: string, public errorCode: string) { + super(message); + } +} diff --git a/src/types/contract.ts b/src/types/contract.ts index 3ff22cf27..5ad3e0c7c 100644 --- a/src/types/contract.ts +++ b/src/types/contract.ts @@ -1,5 +1,6 @@ export type AsyncContractFunction = (...args: Array) => Promise; export type ContractFunction = (...args: Array) => any; -export interface Result extends Array { +export interface ResultAccessor extends Array { [key: string]: any; } +export type Result = ResultAccessor | undefined;