Skip to content

Commit

Permalink
fix(provider): allow the user to handle the contract errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Arcticae committed Jun 20, 2022
1 parent 94b2fff commit 5190f7a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/contract/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
Overrides,
ParsedStruct,
Result,
ResultAccessor,
StructAbi,
} from '../types';
import { BigNumberish, toBN, toFelt } from '../utils/number';
Expand Down Expand Up @@ -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) => {
Expand All @@ -537,7 +541,7 @@ export class Contract implements ContractInterface {
acc.push(value);
acc[key] = value;
return acc;
}, [] as Result);
}, [] as ResultAccessor);
}

public invoke(
Expand Down
8 changes: 5 additions & 3 deletions src/provider/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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();
})
Expand Down
6 changes: 6 additions & 0 deletions src/provider/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
3 changes: 2 additions & 1 deletion src/types/contract.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export type AsyncContractFunction<T = any> = (...args: Array<any>) => Promise<T>;
export type ContractFunction = (...args: Array<any>) => any;
export interface Result extends Array<any> {
export interface ResultAccessor extends Array<any> {
[key: string]: any;
}
export type Result = ResultAccessor | undefined;

0 comments on commit 5190f7a

Please sign in to comment.