diff --git a/src/infrastructure/services/api-clients.ts b/src/infrastructure/services/api-clients.ts index 2b31ef17..072f1c0b 100644 --- a/src/infrastructure/services/api-clients.ts +++ b/src/infrastructure/services/api-clients.ts @@ -1,4 +1,5 @@ import { BaseError, ErrorHelper, PackageErrorCode } from "../../packages/error"; +import { PackageError } from "../../packages/error/package-error"; import {BlockstackDomainId} from "../../packages/identity-utils"; import {getLogger} from "../../packages/logger"; import { StorageService } from "../../packages/storage"; @@ -56,7 +57,10 @@ export class GaiaServiceApiClient { return Boolean(filename !== UPLOADABLE_JSON_FILES.CLIENT_CONFIG || data.indexOf("BlobNotFound") > 0 || data.indexOf("NoSuchKey") > 0); }); } catch (error) { - throw ErrorHelper.getPackageError(null, PackageErrorCode.GaiaGetFileFailed, filename); + if (error instanceof PackageError && error.errorCode === PackageErrorCode.FileNotFound) { + throw ErrorHelper.getPackageError(error, PackageErrorCode.GaiaEmptyResponse); + } + throw ErrorHelper.getPackageError(error, PackageErrorCode.GaiaGetFileFailed, filename); } return responseBody; } diff --git a/src/packages/error/error-string.ts b/src/packages/error/error-string.ts index f14dc551..b74e9124 100644 --- a/src/packages/error/error-string.ts +++ b/src/packages/error/error-string.ts @@ -60,4 +60,5 @@ export const ERROR_STRINGS = { [PackageErrorCode.InvalidWalletClientName]: "Invalid Wallet Client Name", [PackageErrorCode.InsecureNetworkCall]: "Insecure network call", [PackageErrorCode.FailedToGetGaiaUrlFromZonefile]: "Failed to get Gaia URL from user's zonefile: '{0}'", + [PackageErrorCode.FileNotFound]: "File not Found 404", }; diff --git a/src/packages/error/package-error-code.ts b/src/packages/error/package-error-code.ts index 08cd8276..ddd15ee8 100644 --- a/src/packages/error/package-error-code.ts +++ b/src/packages/error/package-error-code.ts @@ -63,4 +63,5 @@ export enum PackageErrorCode { MissingCruxDomainInCruxWalletClient = 5011, MissingZoneFile = 5012, MissingNameOwnerAddress = 5013, + FileNotFound = 5014, } diff --git a/src/packages/utils.ts b/src/packages/utils.ts index a73c64ea..1f74b2af 100644 --- a/src/packages/utils.ts +++ b/src/packages/utils.ts @@ -2,6 +2,7 @@ import * as bitcoin from "bitcoinjs-lib"; import * as cloner from "cloner"; import request from "request"; import { BaseError, ErrorHelper, PackageErrorCode } from "./error"; +import { PackageError } from "./error/package-error"; import { getLogger } from "./logger"; import { StorageService } from "./storage"; @@ -11,15 +12,20 @@ const httpsPrefixRegex = new RegExp(`^https:\/\/.+$`); /* istanbul ignore next */ const httpJSONRequest = (options: (request.UriOptions & request.CoreOptions) | (request.UrlOptions & request.CoreOptions)): Promise => { log.debug("network_call:", options); - const promise: Promise = new Promise((resolve, reject) => { + const promise: Promise = new Promise((resolve, reject) => { const { url, fetchOptions } = translateRequestOptionsToFetchOptions(options); if (!httpsPrefixRegex.test(url)) { throw ErrorHelper.getPackageError(null, PackageErrorCode.InsecureNetworkCall); } fetch(url, fetchOptions) - .then((res) => res.json()) + .then((res) => { + if (res.status === 404) { + throw ErrorHelper.getPackageError(null, PackageErrorCode.FileNotFound); + } + return res.json(); + }) .then((json) => resolve(json)) - .catch((err) => reject(new BaseError(null, err))); + .catch(reject); }); return promise; };