From aab0fabde52039bb3f569ced70cf6e4e2f318b00 Mon Sep 17 00:00:00 2001 From: amansinghbais Date: Wed, 14 Feb 2024 14:16:29 +0530 Subject: [PATCH] Improved: Promise response on api failure --- src/modules/notification/index.ts | 36 +++++++++++++++++++++++++------ src/modules/user/index.ts | 36 +++++++++++++++++++++++++------ 2 files changed, 60 insertions(+), 12 deletions(-) diff --git a/src/modules/notification/index.ts b/src/modules/notification/index.ts index 1365006..33fae20 100644 --- a/src/modules/notification/index.ts +++ b/src/modules/notification/index.ts @@ -24,7 +24,11 @@ async function getNotificationEnumIds(enumTypeId: string): Promise { throw resp.data; } } catch (err) { - return Promise.reject(err) + return Promise.reject({ + code: 'error', + message: 'Something went wrong', + serverResponse: err + }) } } @@ -52,7 +56,11 @@ async function getNotificationUserPrefTypeIds(applicationId: string, userLoginId throw resp.data; } } catch (err) { - return Promise.reject(err) + return Promise.reject({ + code: 'error', + message: 'Something went wrong', + serverResponse: err + }) } } @@ -76,7 +84,11 @@ async function storeClientRegistrationToken(registrationToken: string, deviceId: throw resp.data; } } catch (err) { - return Promise.reject(err) + return Promise.reject({ + code: 'error', + message: 'Something went wrong', + serverResponse: err + }) } } @@ -100,7 +112,11 @@ async function removeClientRegistrationToken(deviceId: string, applicationId: st throw resp.data; } } catch (err) { - return Promise.reject(err) + return Promise.reject({ + code: 'error', + message: 'Something went wrong', + serverResponse: err + }) } } @@ -123,7 +139,11 @@ async function subscribeTopic(topicName: string, applicationId: string): Promise throw resp.data; } } catch (err) { - return Promise.reject(err) + return Promise.reject({ + code: 'error', + message: 'Something went wrong', + serverResponse: err + }) } } @@ -146,7 +166,11 @@ async function unsubscribeTopic(topicName: string, applicationId: string): Promi throw resp.data; } } catch (err) { - return Promise.reject(err) + return Promise.reject({ + code: 'error', + message: 'Something went wrong', + serverResponse: err + }) } } diff --git a/src/modules/user/index.ts b/src/modules/user/index.ts index ed186f5..464cd7e 100644 --- a/src/modules/user/index.ts +++ b/src/modules/user/index.ts @@ -82,10 +82,18 @@ async function setProductIdentificationPref(eComStoreId: string, productIdentifi if(!hasError(resp)) { return Promise.resolve(productIdentificationPref) } else { - return Promise.reject(resp) + return Promise.reject({ + code: 'error', + message: 'Failed to set product identification pref', + serverResponse: resp.data + }) } } catch(err) { - return Promise.reject(err) + return Promise.reject({ + code: 'error', + message: 'Something went wrong', + serverResponse: err + }) } } @@ -171,7 +179,11 @@ async function logout(): Promise { return Promise.resolve(resp.data) } catch(err) { - return Promise.reject(err) + return Promise.reject({ + code: 'error', + message: 'Something went wrong', + serverResponse: err + }) } } @@ -250,7 +262,11 @@ async function getUserPreference(token: any, baseURL: string, userPrefTypeId: st } return Promise.resolve(resp.data.userPrefValue ? JSON.parse(resp.data.userPrefValue) : {}); } catch (err) { - return Promise.reject(err) + return Promise.reject({ + code: 'error', + message: 'Something went wrong', + serverResponse: err + }) } } @@ -268,7 +284,11 @@ async function setUserPreference(payload: any): Promise { throw resp.data } } catch (err) { - return Promise.reject(err) + return Promise.reject({ + code: 'error', + message: 'Something went wrong', + serverResponse: err + }) } } @@ -286,7 +306,11 @@ const setUserLocale = async (payload: any): Promise => { throw resp.data } } catch (error) { - return Promise.reject(error) + return Promise.reject({ + code: 'error', + message: 'Something went wrong', + serverResponse: error + }) } }