diff --git a/packages/fetch-api/src/fetch-error.ts b/packages/fetch-api/src/fetch-error.ts index 2be6dd30..d94c93e3 100644 --- a/packages/fetch-api/src/fetch-error.ts +++ b/packages/fetch-api/src/fetch-error.ts @@ -10,4 +10,6 @@ class FetchError extends Error { } } +export class GeoFetchError extends FetchError {} + export default FetchError diff --git a/packages/fetch-api/src/index.ts b/packages/fetch-api/src/index.ts index f56c5d52..c70d3c63 100644 --- a/packages/fetch-api/src/index.ts +++ b/packages/fetch-api/src/index.ts @@ -2,7 +2,7 @@ import { appendGiphySDKRequestHeader, getGiphySDKRequestHeaders } from '@giphy/j export { default as GiphyFetch } from './api' export { serverUrl, setServerUrl } from './constants' -export { default as FetchError } from './fetch-error' +export { default as FetchError, GeoFetchError } from './fetch-error' export * from './option-types' export { gifPaginator } from './paginator' export { default as request } from './request' diff --git a/packages/fetch-api/src/request.ts b/packages/fetch-api/src/request.ts index 41738b47..cbc1ee84 100644 --- a/packages/fetch-api/src/request.ts +++ b/packages/fetch-api/src/request.ts @@ -1,5 +1,5 @@ import { serverUrl } from './constants' -import FetchError from './fetch-error' +import FetchError, { GeoFetchError } from './fetch-error' import { ErrorResult, Result } from './result-types' export const ERROR_PREFIX = `@giphy/js-fetch-api: ` @@ -60,6 +60,7 @@ function request(url: string, options: RequestOptions = {}) { // error results have a different format than regular results const result = (await response.json()) as ErrorResult if (result.message) message = result.message + if (result.meta?.msg) message = result.meta.msg } catch (_) {} if (requestMap[url]) { // we got a specific error, @@ -69,12 +70,11 @@ function request(url: string, options: RequestOptions = {}) { } // we got an error response, throw with the message in the response body json - fetchError = new FetchError( - `${ERROR_PREFIX}${message}`, - fullUrl, - response.status, - response.statusText - ) + let Cls = FetchError + if (message === 'This content is not available in your location') { + Cls = GeoFetchError + } + fetchError = new Cls(`${ERROR_PREFIX}${message}`, fullUrl, response.status, response.statusText) } } catch (unexpectedError: any) { fetchError = new FetchError(unexpectedError.message, fullUrl)