From 4a61fa4045db930d8d00b353629844e67fdca9de Mon Sep 17 00:00:00 2001 From: Bucur David Date: Mon, 11 Sep 2023 12:51:35 +0300 Subject: [PATCH] refactor: implement custom error class for marketplace Refs: #25 --- src/datanft.ts | 2 +- src/errors.ts | 10 +++++++-- src/marketplace.ts | 53 +++++++++++++++++++++++++++------------------- 3 files changed, 40 insertions(+), 25 deletions(-) diff --git a/src/datanft.ts b/src/datanft.ts index 6dbbe1c..7df3728 100644 --- a/src/datanft.ts +++ b/src/datanft.ts @@ -61,7 +61,7 @@ export class DataNft { * @param env 'devnet' | 'mainnet' | 'testnet' */ static setNetworkConfig(env: string) { - if (env !== 'devnet' && env !== 'mainnet' && env !== 'testnet') { + if (!(env in EnvironmentsEnum)) { throw new ErrNetworkConfig( `Invalid environment: ${env}, Expected: 'devnet' | 'mainnet' | 'testnet'` ); diff --git a/src/errors.ts b/src/errors.ts index 9ca8ee5..1999248 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -21,7 +21,7 @@ export class ErrBadType extends Error { export class ErrDataNftCreate extends Error { public constructor(message?: string) { - super(`Could not create Data NFT: ${message}`); + super(`Failed to create Data NFT: ${message}`); } } @@ -33,7 +33,7 @@ export class ErrFetch extends Error { export class ErrDecodeAttributes extends Error { public constructor(message?: string) { - super(`Could not decode attributes: ${message}`); + super(`Failed to decode attributes: ${message}`); } } @@ -42,3 +42,9 @@ export class ErrAttributeNotSet extends Error { super(`Attribute "${attribute}" is not set`); } } + +export class ErrContractQuery extends Error { + public constructor(method: string, message?: string) { + super(`Failed to query contract: Method: ${method} : ${message}`); + } +} diff --git a/src/marketplace.ts b/src/marketplace.ts index a3c5b38..df29383 100644 --- a/src/marketplace.ts +++ b/src/marketplace.ts @@ -26,7 +26,7 @@ import { import dataMarketAbi from './abis/data_market.abi.json'; import { MarketplaceRequirements, Offer } from './interfaces'; import { parseOffer } from './utils'; -// import { ErrContractQuery } from './errors'; +import { ErrContractQuery, ErrNetworkConfig } from './errors'; export class DataNftMarket { readonly contract: SmartContract; @@ -40,6 +40,11 @@ export class DataNftMarket { * @param timeout Timeout for the network provider (DEFAULT = 10000ms) */ constructor(env: string, timeout: number = 10000) { + if (!(env in EnvironmentsEnum)) { + throw new ErrNetworkConfig( + `Invalid environment: ${env}, Expected: 'devnet' | 'mainnet' | 'testnet'` + ); + } this.env = env; const networkConfig = networkConfiguration[env as EnvironmentsEnum]; this.chainID = networkConfig.chainID; @@ -87,7 +92,10 @@ export class DataNftMarket { ); return offers; } else { - return []; + throw new ErrContractQuery( + 'viewAddressListedOffers', + returnCode.toString() + ); } } @@ -122,7 +130,10 @@ export class DataNftMarket { ); return offers; } else { - return []; + throw new ErrContractQuery( + 'viewAddressPagedOffers', + returnCode.toString() + ); } } @@ -145,7 +156,10 @@ export class DataNftMarket { const returnValue = firstValue?.valueOf(); return returnValue.toNumber(); } else { - return 0; + throw new ErrContractQuery( + 'viewAddressTotalOffers', + returnCode.toString() + ); } } @@ -171,7 +185,10 @@ export class DataNftMarket { ); return offers; } else { - return []; + throw new ErrContractQuery( + 'viewAddressCancelledOffers', + returnCode.toString() + ); } } @@ -199,7 +216,7 @@ export class DataNftMarket { ); return offers; } else { - return []; + throw new ErrContractQuery('viewPagedOffers', returnCode.toString()); } } @@ -248,7 +265,7 @@ export class DataNftMarket { ); return offers; } else { - return []; + throw new ErrContractQuery('viewOffers', returnCode.toString()); } } @@ -281,10 +298,7 @@ export class DataNftMarket { }; return requirements; } else { - throw new Error('Error while retrieving the marketplace requirements'); - // throw new ErrContractQuery( - // 'Error while retrieving the marketplace requirements' - // ); + throw new ErrContractQuery('viewRequirements', returnCode.toString()); } } @@ -304,9 +318,7 @@ export class DataNftMarket { const returnValue = firstValue?.valueOf(); return new U8Value(returnValue).valueOf().toNumber(); } - - throw new Error('Error while retrieving the number of offers'); - // throw new ErrContractQuery('Error while retrieving the number of offers'); + throw new ErrContractQuery('viewNumberOfOffers', returnCode.toString()); } /** @@ -326,10 +338,7 @@ export class DataNftMarket { return new U64Value(returnValue).valueOf().toNumber(); } - throw new Error('Error while retrieving the last valid offer id'); - // throw new ErrContractQuery( - // 'Error while retrieving the last valid offer id' - // ); + throw new ErrContractQuery('viewLastValidOfferId', returnCode.toString()); } /** @@ -348,10 +357,10 @@ export class DataNftMarket { const returnValue = firstValue?.valueOf(); return new BooleanValue(returnValue).valueOf(); } else { - throw new Error('Error while retrieving the contract pause state'); - // throw new ErrContractQuery( - // 'Error while retrieving the contract pause state' - // ); + throw new ErrContractQuery( + 'viewContractPauseState', + returnCode.toString() + ); } }