From c1c4d1272bd973131b2e5651f6710964c779207e Mon Sep 17 00:00:00 2001 From: Abhinav-grd Date: Mon, 31 May 2021 16:28:33 +0530 Subject: [PATCH] added error handling to file delete --- src/pages/gallery/index.tsx | 34 +++++++++++++++++++++----- src/services/fileService.ts | 1 + src/utils/common/errorUtil.ts | 1 + src/utils/strings/englishConstants.tsx | 1 + 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/pages/gallery/index.tsx b/src/pages/gallery/index.tsx index c5d76c451..de1a29450 100644 --- a/src/pages/gallery/index.tsx +++ b/src/pages/gallery/index.tsx @@ -278,13 +278,35 @@ export default function Gallery() { addToCollectionHelper(collectionName, null), }); - const deleteFileHelper = () => { + const deleteFileHelper = async () => { loadingBar.current?.continuousStart(); - deleteFiles( - getSelectedFileIds(selected), - clearSelection, - syncWithRemote - ); + try { + await deleteFiles( + getSelectedFileIds(selected), + clearSelection, + syncWithRemote, + + ); + } catch (e) { + loadingBar.current.complete(); + switch (e.status?.toString()) { + case errorCodes.ERR_FORBIDDEN: + setDialogMessage({ + title: constants.ERROR, + staticBackdrop: true, + close: { variant: 'danger' }, + content: constants.NOT_FILE_OWNER, + }); + loadingBar.current.complete(); + return; + } + setDialogMessage({ + title: constants.ERROR, + staticBackdrop: true, + close: { variant: 'danger' }, + content: constants.UNKNOWN_ERROR, + }); + } }; const updateFiles = (files: File[]) => { setFiles(files); diff --git a/src/services/fileService.ts b/src/services/fileService.ts index b509124d1..63afd1a60 100644 --- a/src/services/fileService.ts +++ b/src/services/fileService.ts @@ -187,5 +187,6 @@ export const deleteFiles = async ( syncWithRemote(); } catch (e) { console.error('delete failed'); + throw e; } }; diff --git a/src/utils/common/errorUtil.ts b/src/utils/common/errorUtil.ts index 04d472d64..790978308 100644 --- a/src/utils/common/errorUtil.ts +++ b/src/utils/common/errorUtil.ts @@ -6,6 +6,7 @@ export const errorCodes = { ERR_NO_INTERNET_CONNECTION: '1', ERR_SESSION_EXPIRED: '401', ERR_KEY_MISSING: '2', + ERR_FORBIDDEN: '403', }; const AXIOS_NETWORK_ERROR = 'Network Error'; diff --git a/src/utils/strings/englishConstants.tsx b/src/utils/strings/englishConstants.tsx index 6954a96f7..711d43e14 100644 --- a/src/utils/strings/englishConstants.tsx +++ b/src/utils/strings/englishConstants.tsx @@ -331,6 +331,7 @@ const englishConstants = { seconds ) ), + NOT_FILE_OWNER: 'deleting shared collection files is not allowed', }; export default englishConstants;