From 7eebc2e3ebb3e0b77b7b6bc164d77608c0ece0eb Mon Sep 17 00:00:00 2001 From: Sanchay Date: Thu, 6 Feb 2020 17:07:47 +0530 Subject: [PATCH 1/2] Added 404 File Not Found Error --- src/infrastructure/services/api-clients.ts | 6 +++++- src/packages/error/error-string.ts | 1 + src/packages/error/package-error-code.ts | 1 + src/packages/utils.ts | 12 +++++++++--- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/infrastructure/services/api-clients.ts b/src/infrastructure/services/api-clients.ts index 2b31ef17..763d5460 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) { + 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..44d2660e 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((err) => reject(err)); }); return promise; }; From d841d0ff7c99fed6211e5449664dbe38a52b8e7b Mon Sep 17 00:00:00 2001 From: Sanchay Date: Fri, 14 Feb 2020 12:06:35 +0530 Subject: [PATCH 2/2] Catching specific error FileNotFound now --- src/infrastructure/services/api-clients.ts | 2 +- src/packages/utils.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/infrastructure/services/api-clients.ts b/src/infrastructure/services/api-clients.ts index 763d5460..072f1c0b 100644 --- a/src/infrastructure/services/api-clients.ts +++ b/src/infrastructure/services/api-clients.ts @@ -57,7 +57,7 @@ export class GaiaServiceApiClient { return Boolean(filename !== UPLOADABLE_JSON_FILES.CLIENT_CONFIG || data.indexOf("BlobNotFound") > 0 || data.indexOf("NoSuchKey") > 0); }); } catch (error) { - if (error instanceof PackageError) { + if (error instanceof PackageError && error.errorCode === PackageErrorCode.FileNotFound) { throw ErrorHelper.getPackageError(error, PackageErrorCode.GaiaEmptyResponse); } throw ErrorHelper.getPackageError(error, PackageErrorCode.GaiaGetFileFailed, filename); diff --git a/src/packages/utils.ts b/src/packages/utils.ts index 44d2660e..1f74b2af 100644 --- a/src/packages/utils.ts +++ b/src/packages/utils.ts @@ -25,7 +25,7 @@ const httpJSONRequest = (options: (request.UriOptions & request.CoreOptions) | ( return res.json(); }) .then((json) => resolve(json)) - .catch((err) => reject(err)); + .catch(reject); }); return promise; };