Skip to content

Commit

Permalink
refactor: implement custom error class for marketplace
Browse files Browse the repository at this point in the history
Refs: #25
  • Loading branch information
bucurdavid committed Sep 11, 2023
1 parent 8550236 commit 4a61fa4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/datanft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'`
);
Expand Down
10 changes: 8 additions & 2 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
}

Expand All @@ -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}`);
}
}

Expand All @@ -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}`);
}
}
53 changes: 31 additions & 22 deletions src/marketplace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -87,7 +92,10 @@ export class DataNftMarket {
);
return offers;
} else {
return [];
throw new ErrContractQuery(
'viewAddressListedOffers',
returnCode.toString()
);
}
}

Expand Down Expand Up @@ -122,7 +130,10 @@ export class DataNftMarket {
);
return offers;
} else {
return [];
throw new ErrContractQuery(
'viewAddressPagedOffers',
returnCode.toString()
);
}
}

Expand All @@ -145,7 +156,10 @@ export class DataNftMarket {
const returnValue = firstValue?.valueOf();
return returnValue.toNumber();
} else {
return 0;
throw new ErrContractQuery(
'viewAddressTotalOffers',
returnCode.toString()
);
}
}

Expand All @@ -171,7 +185,10 @@ export class DataNftMarket {
);
return offers;
} else {
return [];
throw new ErrContractQuery(
'viewAddressCancelledOffers',
returnCode.toString()
);
}
}

Expand Down Expand Up @@ -199,7 +216,7 @@ export class DataNftMarket {
);
return offers;
} else {
return [];
throw new ErrContractQuery('viewPagedOffers', returnCode.toString());
}
}

Expand Down Expand Up @@ -248,7 +265,7 @@ export class DataNftMarket {
);
return offers;
} else {
return [];
throw new ErrContractQuery('viewOffers', returnCode.toString());
}
}

Expand Down Expand Up @@ -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());
}
}

Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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()
);
}
}

Expand Down

0 comments on commit 4a61fa4

Please sign in to comment.