Skip to content

Commit

Permalink
better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
borispoehland committed Oct 4, 2024
1 parent 9c9ca40 commit 128240d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/interactions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export class SCInteraction {
token: string,
nonce: number
): Promise<number> {
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])
)
Expand Down
46 changes: 24 additions & 22 deletions src/users/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class UserModule {
* @returns {IUserProfile}
*/
public getUserProfile = async (address: string): Promise<IUserProfile> => {
if (!isAddressValid(address)) throw new Error('Invalid address')
if (!isAddressValid(address)) throw new Error('Invalid address:' + address)
const response = await this.api.fetchWithTimeout<IUserProfile>(
`/user/${address}/profile`
)
Expand Down Expand Up @@ -85,7 +85,7 @@ export class UserModule {
public getUserAccount = async (
address: string
): Promise<UserNetworkAccount> => {
if (!isAddressValid(address)) throw new Error('Invalid address')
if (!isAddressValid(address)) throw new Error('Invalid address:' + address)
const response = await this.api.fetchWithTimeout<UserNetworkAccount>(
`/user/${address}/network-account`
)
Expand All @@ -101,7 +101,7 @@ export class UserModule {
public getUserTokenInventory = async (
address: string
): Promise<UserTokenInventory> => {
if (!isAddressValid(address)) throw new Error('Invalid address')
if (!isAddressValid(address)) throw new Error('Invalid address:' + address)
const response = await this.api.fetchWithTimeout<UserTokenInventory>(
`/user/${address}/token-inventory`
)
Expand All @@ -119,7 +119,7 @@ export class UserModule {
address: string,
activeAuctions = true
): Promise<UserInventory[]> => {
if (!isAddressValid(address)) throw new Error('Invalid address')
if (!isAddressValid(address)) throw new Error('Invalid address:' + address)
const response = await this.api.fetchWithTimeout<UserInventory[]>(
`/user/${address}/inventory-summary?activeAuction=${activeAuctions}`
)
Expand All @@ -144,7 +144,8 @@ export class UserModule {
* @returns {UserOffers} - The user's listings
*/
public getUserOffers = async (args: ArgsUserOffers): Promise<UserOffers> => {
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<UserOffers>(
`/user/${args.address}/offers`,
{
Expand Down Expand Up @@ -217,7 +218,7 @@ export class UserModule {
public getUserCreatorProfile = async (
address: string
): Promise<CreatorProfile> => {
if (!isAddressValid(address)) throw new Error('Invalid address')
if (!isAddressValid(address)) throw new Error('Invalid address:' + address)

const response = await this.api.fetchWithTimeout<CreatorProfile>(
`/user/${address}/creator/profile`
Expand All @@ -233,7 +234,7 @@ export class UserModule {
public getCreatorListings = async (
address: string
): Promise<CollectionCreatorInfo> => {
if (!isAddressValid(address)) throw new Error('Invalid address')
if (!isAddressValid(address)) throw new Error('Invalid address:' + address)

const response = await this.api.fetchWithTimeout<CollectionCreatorInfo>(
`/user/${address}/creator/listing`
Expand All @@ -250,7 +251,7 @@ export class UserModule {
address: string,
extra?: RequestInit
): Promise<EventCreatorInfo> => {
if (!isAddressValid(address)) throw new Error('Invalid address')
if (!isAddressValid(address)) throw new Error('Invalid address:' + address)

const response = await this.api.fetchWithTimeout<EventCreatorInfo>(
`/user/${address}/creator/events`,
Expand All @@ -269,7 +270,7 @@ export class UserModule {
public getUserStakingSummary = async (
address: string
): Promise<StakingSummaryPoolsSlim[]> => {
if (!isAddressValid(address)) throw new Error('Invalid address')
if (!isAddressValid(address)) throw new Error('Invalid address:' + address)

const response = await this.api.fetchWithTimeout<StakingSummaryPoolsSlim[]>(
`/user/${address}/staking/summary`
Expand All @@ -285,7 +286,7 @@ export class UserModule {
public getUserStakingAailable = async (
address: string
): Promise<StakingSummaryPools[]> => {
if (!isAddressValid(address)) throw new Error('Invalid address')
if (!isAddressValid(address)) throw new Error('Invalid address:' + address)

const response = await this.api.fetchWithTimeout<StakingSummaryPools[]>(
`/user/${address}/staking/available-pools`
Expand All @@ -302,7 +303,7 @@ export class UserModule {
address: string,
extra?: RequestInit
): Promise<CreatorInfo> => {
if (!isAddressValid(address)) throw new Error('Invalid address')
if (!isAddressValid(address)) throw new Error('Invalid address:' + address)

const response = await this.api.fetchWithTimeout<CreatorInfo>(
`/user/${address}/creator/details`,
Expand All @@ -323,7 +324,7 @@ export class UserModule {
address: string,
collection: string
): Promise<StakingSummaryPools[]> => {
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)
}
Expand All @@ -343,7 +344,7 @@ export class UserModule {
poolId: number,
status: StakingStatus
): Promise<UserPoolStakingInfo> => {
if (!isAddressValid(address)) throw new Error('Invalid address')
if (!isAddressValid(address)) throw new Error('Invalid address:' + address)
const response = await this.api.fetchWithTimeout<UserPoolStakingInfo>(
`/user/${address}/staking/pool/${poolId}/nfts?status=${status}`
)
Expand All @@ -358,7 +359,7 @@ export class UserModule {
public getOwnedPoolsByAddress = async (
address: string
): Promise<StakingSummaryPools[]> => {
if (!isAddressValid(address)) throw new Error('Invalid address')
if (!isAddressValid(address)) throw new Error('Invalid address:' + address)
const response = await this.api.fetchWithTimeout<StakingSummaryPools[]>(
`/user/${address}/staking/owned-pools`
)
Expand All @@ -375,7 +376,7 @@ export class UserModule {
public getUserAnalyticsSummary = async (
address: string
): Promise<UserAnalyticSummary> => {
if (!isAddressValid(address)) throw new Error('Invalid address')
if (!isAddressValid(address)) throw new Error('Invalid address:' + address)
const response = await this.api.fetchWithTimeout<UserAnalyticSummary>(
`/user/${address}/analytics/volume`
)
Expand All @@ -394,7 +395,7 @@ export class UserModule {
top: number,
skip: number
): Promise<SearchNFTsResponse> => {
if (!isAddressValid(address)) throw new Error('Invalid address')
if (!isAddressValid(address)) throw new Error('Invalid address:' + address)

const response = await this.api.fetchWithTimeout<SearchNFTsResponse>(
`/user/${address}/favorite/nfts`,
Expand All @@ -416,7 +417,7 @@ export class UserModule {
public getUserFavoriteCollectionTickers = async (
address: string
): Promise<string[]> => {
if (!isAddressValid(address)) throw new Error('Invalid address')
if (!isAddressValid(address)) throw new Error('Invalid address:' + address)

const response = await this.api.fetchWithTimeout<string[]>(
`/user/${address}/favorite/collections`
Expand Down Expand Up @@ -479,7 +480,7 @@ export class UserModule {
public getStakingCreatorInfo = async (
address: string
): Promise<StakingCreatorInfo> => {
if (!isAddressValid(address)) throw new Error('Invalid address')
if (!isAddressValid(address)) throw new Error('Invalid address:' + address)
return await this.api.fetchWithTimeout<StakingCreatorInfo>(
`/user/${address}/staking/creator`,
{
Expand Down Expand Up @@ -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<UserXOXNODrop[]>(
Expand All @@ -538,7 +540,7 @@ export class UserModule {
public getUserOwnerCollections = async (
address: string
): Promise<IOwnerInfo> => {
if (!isAddressValid(address)) throw new Error('Invalid address')
if (!isAddressValid(address)) throw new Error('Invalid address:' + address)
const response = await this.api.fetchWithTimeout<IOwnerInfo>(
`/user/${address}/staking/owned-collections`
)
Expand All @@ -553,7 +555,7 @@ export class UserModule {
public getRoyaltiesSharesCreator = async (
address: string
): Promise<IApiShareholder[]> => {
if (!isAddressValid(address)) throw new Error('Invalid address')
if (!isAddressValid(address)) throw new Error('Invalid address:' + address)
const response = await this.api.fetchWithTimeout<IApiShareholder[]>(
`/launchpad/${address}/shareholders/royalties`
)
Expand All @@ -569,7 +571,7 @@ export class UserModule {
address: string,
collectionTag: string
): Promise<IApiShareholder[]> => {
if (!isAddressValid(address)) throw new Error('Invalid address')
if (!isAddressValid(address)) throw new Error('Invalid address:' + address)
const response = await this.api.fetchWithTimeout<IApiShareholder[]>(
`/launchpad/${address}/shareholders/collection/${collectionTag}`
)
Expand Down
18 changes: 15 additions & 3 deletions src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 128240d

Please sign in to comment.