From 72e9b1cfa29d6a3199ed03517ac1f79f85f69d9e Mon Sep 17 00:00:00 2001 From: Abhinav-grd Date: Mon, 22 Feb 2021 16:50:08 +0530 Subject: [PATCH] added error handler adn grouped errorCode and handler in errorUtil file --- src/pages/gallery/components/AlertBanner.tsx | 2 +- src/services/uploadService.ts | 17 +++++++---------- src/utils/common/errorCodes.ts | 7 ------- src/utils/common/errorUtil.ts | 19 +++++++++++++++++++ src/utils/common/utilFunctions.ts | 2 +- 5 files changed, 28 insertions(+), 19 deletions(-) delete mode 100644 src/utils/common/errorCodes.ts create mode 100644 src/utils/common/errorUtil.ts diff --git a/src/pages/gallery/components/AlertBanner.tsx b/src/pages/gallery/components/AlertBanner.tsx index 7bd10556d..38ac81346 100644 --- a/src/pages/gallery/components/AlertBanner.tsx +++ b/src/pages/gallery/components/AlertBanner.tsx @@ -2,7 +2,7 @@ import React from 'react'; import { Alert } from 'react-bootstrap'; import constants from 'utils/strings/constants'; -import errorCodes from 'utils/common/errorCodes'; +import {errorCodes} from 'utils/common/errorUtil'; export default function AlertBanner({ bannerErrorCode }) { let errorMessage; diff --git a/src/services/uploadService.ts b/src/services/uploadService.ts index 08ff3ea80..58445746d 100644 --- a/src/services/uploadService.ts +++ b/src/services/uploadService.ts @@ -5,8 +5,8 @@ import EXIF from 'exif-js'; import { fileAttribute } from './fileService'; import { collection, CollectionAndItsLatestFile } from './collectionService'; import { FILE_TYPE } from 'pages/gallery'; -import errorCodes from 'utils/common/errorCodes'; import { checkConnectivity } from 'utils/common/utilFunctions'; +import { ErrorHandler } from 'utils/common/errorUtil'; const CryptoWorker: any = typeof window !== 'undefined' && Comlink.wrap(new Worker('worker/crypto.worker.js', { type: 'module' })); @@ -125,7 +125,11 @@ class UploadService { progressBarProps.setUploadStage(UPLOAD_STAGES.UPLOADING); this.changeProgressBarProps(); - await this.fetchUploadURLs(token); + try { + await this.fetchUploadURLs(token); + } catch (e) { + ErrorHandler(e); + } const uploadProcesses = []; for (let i = 0; i < Math.min(5, this.totalFileCount); i++) { uploadProcesses.push( @@ -168,14 +172,7 @@ class UploadService { this.filesCompleted++; this.changeProgressBarProps(); } catch (e) { - if ( - e.response?.status.toString() == - errorCodes.ERR_STORAGE_LIMIT_EXCEEDED || - e.response?.status.toString() == - errorCodes.ERR_NO_ACTIVE_SUBSRICTION - ) { - throw new Error(e.response.status); - } + ErrorHandler(e); const error = new Error( `Uploading Failed for File - ${rawFile.name}` ); diff --git a/src/utils/common/errorCodes.ts b/src/utils/common/errorCodes.ts deleted file mode 100644 index 42a1fb435..000000000 --- a/src/utils/common/errorCodes.ts +++ /dev/null @@ -1,7 +0,0 @@ -const errorCodes = { - ERR_STORAGE_LIMIT_EXCEEDED: '426', - ERR_NO_ACTIVE_SUBSCRIPTION: '402', - ERR_NO_INTERNET_CONNECTION: '1', -}; - -export default errorCodes; diff --git a/src/utils/common/errorUtil.ts b/src/utils/common/errorUtil.ts new file mode 100644 index 000000000..4979b0bfc --- /dev/null +++ b/src/utils/common/errorUtil.ts @@ -0,0 +1,19 @@ +export const errorCodes = { + ERR_STORAGE_LIMIT_EXCEEDED: '426', + ERR_NO_ACTIVE_SUBSCRIPTION: '402', + ERR_NO_INTERNET_CONNECTION: '1', +}; + +export function ErrorHandler(error) { + if (error.res) + if ( + error.response?.status.toString() == + errorCodes.ERR_STORAGE_LIMIT_EXCEEDED || + error.response?.status.toString() == + errorCodes.ERR_NO_ACTIVE_SUBSCRIPTION + ) { + throw new Error(error.response.status); + } else { + return; + } +} diff --git a/src/utils/common/utilFunctions.ts b/src/utils/common/utilFunctions.ts index 8c6b837df..73a343ce7 100644 --- a/src/utils/common/utilFunctions.ts +++ b/src/utils/common/utilFunctions.ts @@ -1,4 +1,4 @@ -import errorCodes from './errorCodes'; +import errorCodes from './errorUtil'; export function checkConnectivity() { if (navigator.onLine) {