diff --git a/src/interactions/index.ts b/src/interactions/index.ts index 90a833c..fd08dc3 100644 --- a/src/interactions/index.ts +++ b/src/interactions/index.ts @@ -135,7 +135,7 @@ export class SCInteraction { token: string, nonce: number ): Promise { - if (!isAddressValid(address)) throw new Error('Invalid address') + if (!isAddressValid(address)) throw new Error('Invalid address:' + address) const result = await this.getResult( this.xo.methods.userDeposit([address, token, nonce]) ) diff --git a/src/users/index.ts b/src/users/index.ts index 3ba4a4d..f80625a 100644 --- a/src/users/index.ts +++ b/src/users/index.ts @@ -50,7 +50,7 @@ export class UserModule { * @returns {IUserProfile} */ public getUserProfile = async (address: string): Promise => { - if (!isAddressValid(address)) throw new Error('Invalid address') + if (!isAddressValid(address)) throw new Error('Invalid address:' + address) const response = await this.api.fetchWithTimeout( `/user/${address}/profile` ) @@ -85,7 +85,7 @@ export class UserModule { public getUserAccount = async ( address: string ): Promise => { - if (!isAddressValid(address)) throw new Error('Invalid address') + if (!isAddressValid(address)) throw new Error('Invalid address:' + address) const response = await this.api.fetchWithTimeout( `/user/${address}/network-account` ) @@ -101,7 +101,7 @@ export class UserModule { public getUserTokenInventory = async ( address: string ): Promise => { - if (!isAddressValid(address)) throw new Error('Invalid address') + if (!isAddressValid(address)) throw new Error('Invalid address:' + address) const response = await this.api.fetchWithTimeout( `/user/${address}/token-inventory` ) @@ -119,7 +119,7 @@ export class UserModule { address: string, activeAuctions = true ): Promise => { - if (!isAddressValid(address)) throw new Error('Invalid address') + if (!isAddressValid(address)) throw new Error('Invalid address:' + address) const response = await this.api.fetchWithTimeout( `/user/${address}/inventory-summary?activeAuction=${activeAuctions}` ) @@ -144,7 +144,8 @@ export class UserModule { * @returns {UserOffers} - The user's listings */ public getUserOffers = async (args: ArgsUserOffers): Promise => { - if (!isAddressValid(args.address)) throw new Error('Invalid address') + if (!isAddressValid(args.address)) + throw new Error('Invalid address:' + args.address) const response = await this.api.fetchWithTimeout( `/user/${args.address}/offers`, { @@ -217,7 +218,7 @@ export class UserModule { public getUserCreatorProfile = async ( address: string ): Promise => { - if (!isAddressValid(address)) throw new Error('Invalid address') + if (!isAddressValid(address)) throw new Error('Invalid address:' + address) const response = await this.api.fetchWithTimeout( `/user/${address}/creator/profile` @@ -233,7 +234,7 @@ export class UserModule { public getCreatorListings = async ( address: string ): Promise => { - if (!isAddressValid(address)) throw new Error('Invalid address') + if (!isAddressValid(address)) throw new Error('Invalid address:' + address) const response = await this.api.fetchWithTimeout( `/user/${address}/creator/listing` @@ -250,7 +251,7 @@ export class UserModule { address: string, extra?: RequestInit ): Promise => { - if (!isAddressValid(address)) throw new Error('Invalid address') + if (!isAddressValid(address)) throw new Error('Invalid address:' + address) const response = await this.api.fetchWithTimeout( `/user/${address}/creator/events`, @@ -269,7 +270,7 @@ export class UserModule { public getUserStakingSummary = async ( address: string ): Promise => { - if (!isAddressValid(address)) throw new Error('Invalid address') + if (!isAddressValid(address)) throw new Error('Invalid address:' + address) const response = await this.api.fetchWithTimeout( `/user/${address}/staking/summary` @@ -285,7 +286,7 @@ export class UserModule { public getUserStakingAailable = async ( address: string ): Promise => { - if (!isAddressValid(address)) throw new Error('Invalid address') + if (!isAddressValid(address)) throw new Error('Invalid address:' + address) const response = await this.api.fetchWithTimeout( `/user/${address}/staking/available-pools` @@ -302,7 +303,7 @@ export class UserModule { address: string, extra?: RequestInit ): Promise => { - if (!isAddressValid(address)) throw new Error('Invalid address') + if (!isAddressValid(address)) throw new Error('Invalid address:' + address) const response = await this.api.fetchWithTimeout( `/user/${address}/creator/details`, @@ -323,7 +324,7 @@ export class UserModule { address: string, collection: string ): Promise => { - if (!isAddressValid(address)) throw new Error('Invalid address') + if (!isAddressValid(address)) throw new Error('Invalid address:' + address) if (!isValidCollectionTicker(collection)) { throw new Error('Invalid collection ticker: ' + collection) } @@ -343,7 +344,7 @@ export class UserModule { poolId: number, status: StakingStatus ): Promise => { - if (!isAddressValid(address)) throw new Error('Invalid address') + if (!isAddressValid(address)) throw new Error('Invalid address:' + address) const response = await this.api.fetchWithTimeout( `/user/${address}/staking/pool/${poolId}/nfts?status=${status}` ) @@ -358,7 +359,7 @@ export class UserModule { public getOwnedPoolsByAddress = async ( address: string ): Promise => { - if (!isAddressValid(address)) throw new Error('Invalid address') + if (!isAddressValid(address)) throw new Error('Invalid address:' + address) const response = await this.api.fetchWithTimeout( `/user/${address}/staking/owned-pools` ) @@ -375,7 +376,7 @@ export class UserModule { public getUserAnalyticsSummary = async ( address: string ): Promise => { - if (!isAddressValid(address)) throw new Error('Invalid address') + if (!isAddressValid(address)) throw new Error('Invalid address:' + address) const response = await this.api.fetchWithTimeout( `/user/${address}/analytics/volume` ) @@ -394,7 +395,7 @@ export class UserModule { top: number, skip: number ): Promise => { - if (!isAddressValid(address)) throw new Error('Invalid address') + if (!isAddressValid(address)) throw new Error('Invalid address:' + address) const response = await this.api.fetchWithTimeout( `/user/${address}/favorite/nfts`, @@ -416,7 +417,7 @@ export class UserModule { public getUserFavoriteCollectionTickers = async ( address: string ): Promise => { - if (!isAddressValid(address)) throw new Error('Invalid address') + if (!isAddressValid(address)) throw new Error('Invalid address:' + address) const response = await this.api.fetchWithTimeout( `/user/${address}/favorite/collections` @@ -479,7 +480,7 @@ export class UserModule { public getStakingCreatorInfo = async ( address: string ): Promise => { - if (!isAddressValid(address)) throw new Error('Invalid address') + if (!isAddressValid(address)) throw new Error('Invalid address:' + address) return await this.api.fetchWithTimeout( `/user/${address}/staking/creator`, { @@ -511,7 +512,8 @@ export class UserModule { } if (address) { - if (!isAddressValid(address)) throw new Error('Invalid address') + if (!isAddressValid(address)) + throw new Error('Invalid address:' + address) } return await this.api.fetchWithTimeout( @@ -538,7 +540,7 @@ export class UserModule { public getUserOwnerCollections = async ( address: string ): Promise => { - if (!isAddressValid(address)) throw new Error('Invalid address') + if (!isAddressValid(address)) throw new Error('Invalid address:' + address) const response = await this.api.fetchWithTimeout( `/user/${address}/staking/owned-collections` ) @@ -553,7 +555,7 @@ export class UserModule { public getRoyaltiesSharesCreator = async ( address: string ): Promise => { - if (!isAddressValid(address)) throw new Error('Invalid address') + if (!isAddressValid(address)) throw new Error('Invalid address:' + address) const response = await this.api.fetchWithTimeout( `/launchpad/${address}/shareholders/royalties` ) @@ -569,7 +571,7 @@ export class UserModule { address: string, collectionTag: string ): Promise => { - if (!isAddressValid(address)) throw new Error('Invalid address') + if (!isAddressValid(address)) throw new Error('Invalid address:' + address) const response = await this.api.fetchWithTimeout( `/launchpad/${address}/shareholders/collection/${collectionTag}` ) diff --git a/src/utils/api.ts b/src/utils/api.ts index 7a78cb4..1f594e0 100644 --- a/src/utils/api.ts +++ b/src/utils/api.ts @@ -130,8 +130,20 @@ export class XOXNOClient { ...(Object.keys(headers).length ? { headers } : {}), method: (options.method as any) ?? 'GET', }) - const consumed = await res.json() - if (!res.ok) throw new Error(consumed.message.toString()) - return consumed as T + if (!res.ok) { + const text = await res.text() + + let message + + try { + message = JSON.parse(text) + } catch (_error) { + message = text + } + + throw new Error(message) + } + + return res.json() as T } }