From 314e13929359eab4eb76229267dd7383cba62afe Mon Sep 17 00:00:00 2001 From: Pierre Bertet Date: Mon, 26 Jul 2021 14:53:36 +0100 Subject: [PATCH] Fix an issue where the error was sometimes an array of errors --- src/fetchers/ethereum/standard-nft.tsx | 10 ++++++++-- src/fetchers/ethers/standard-nft.tsx | 10 ++++++++-- src/utils.tsx | 10 ++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/fetchers/ethereum/standard-nft.tsx b/src/fetchers/ethereum/standard-nft.tsx index bcb3d99..d856ad2 100644 --- a/src/fetchers/ethereum/standard-nft.tsx +++ b/src/fetchers/ethereum/standard-nft.tsx @@ -2,7 +2,7 @@ import type { Address, FetchContext, NftMetadata } from "../../types" import type { EthereumFetcherConfig, EthereumProviderEip1193 } from "./types" import { fetchMetadata } from "../shared/fetch-metadata" -import { normalizeTokenUrl, promiseAny } from "../../utils" +import { MultipleErrors, normalizeTokenUrl, promiseAny } from "../../utils" import { decodeAddress, decodeString, @@ -26,7 +26,13 @@ async function url( uriMethods(tokenId).map((method) => ethCall(ethereum, contractAddress, method) ) - ) + ).catch((errors) => { + throw new MultipleErrors( + "An error occurred while trying to fetch the token URI from the NFT" + + " contract. See the “errors” property on this error for details.", + errors + ) + }) return normalizeTokenUrl(decodeString(uri), tokenId, fetchContext) } diff --git a/src/fetchers/ethers/standard-nft.tsx b/src/fetchers/ethers/standard-nft.tsx index 10adea3..79b1bbd 100644 --- a/src/fetchers/ethers/standard-nft.tsx +++ b/src/fetchers/ethers/standard-nft.tsx @@ -3,7 +3,7 @@ import type { Address, FetchContext, NftMetadata } from "../../types" import type { EthersFetcherConfig } from "./types" import { fetchMetadata } from "../shared/fetch-metadata" -import { normalizeTokenUrl, promiseAny } from "../../utils" +import { MultipleErrors, normalizeTokenUrl, promiseAny } from "../../utils" const ABI = [ // ERC-721 @@ -27,7 +27,13 @@ async function url( const uri = await promiseAny([ contract.tokenURI(tokenId), contract.uri(tokenId), - ]) + ]).catch((errors) => { + throw new MultipleErrors( + "An error occurred while trying to fetch the token URI from the NFT" + + " contract. See the “errors” property on this error for details.", + errors + ) + }) return normalizeTokenUrl(uri, tokenId, fetchContext) } diff --git a/src/utils.tsx b/src/utils.tsx index 2a01a8e..486693e 100644 --- a/src/utils.tsx +++ b/src/utils.tsx @@ -251,3 +251,13 @@ export function reversePromise(promise: Promise): Promise { Promise.resolve(promise).then(reject, resolve) }) } + +// To replace with AggregateError when useNft() will target ES2021 environments +export class MultipleErrors extends Error { + errors: Error[] + constructor(message: string, errors: Error[]) { + super(message) + this.name = "MultipleErrors" + this.errors = errors + } +}