From b349e450c1aa3f344c1526942c531f3fc95a0f84 Mon Sep 17 00:00:00 2001 From: Joy A Date: Wed, 28 Aug 2024 18:47:28 +0530 Subject: [PATCH] #1460 | Catchment not setup for user - error handling --- .../src/service/ServerError.js | 15 ++++- .../openchs-android/src/views/LoginView.js | 59 ++++++++----------- 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/packages/openchs-android/src/service/ServerError.js b/packages/openchs-android/src/service/ServerError.js index 317379b4a..32c730465 100644 --- a/packages/openchs-android/src/service/ServerError.js +++ b/packages/openchs-android/src/service/ServerError.js @@ -1,6 +1,7 @@ import ErrorUtil from "../framework/errorHandling/ErrorUtil"; import AvniError from "../framework/errorHandling/AvniError"; import _ from "lodash"; +import {ErrorCodes} from "openchs-models"; function ServerError(response) { const instance = new Error(response); @@ -36,6 +37,9 @@ function getServerStatusMessageKey(serverError) { } } +const knownServerStatusMessages = ['serverUnavailableTryLater']; +const knownServerExceptionMessages = ['NoCatchmentFound']; + export function getAvniError(serverError, i18n) { const statusCode = getStatusCode(serverError); const errorCode = _.isNil(statusCode) ? "" : `Http ${statusCode}`; @@ -43,8 +47,15 @@ export function getAvniError(serverError, i18n) { const serverErrorPromise = serverError.response.text() || Promise.resolve(i18n.t("unknownServerErrorReason")); return serverErrorPromise.then((errorMessage) => { const statusMessageKey = getServerStatusMessageKey(serverError); - if (statusMessageKey === 'serverUnavailableTryLater') avniError.showOnlyUserMessage = true; - avniError.userMessage = `${i18n.t(statusMessageKey)}. ${errorCode}. ${errorMessage}`; + if (_.includes(knownServerStatusMessages, statusMessageKey)) { + avniError.userMessage = `${i18n.t(statusMessageKey)}`; + avniError.showOnlyUserMessage = true; + } else if (_.includes(knownServerExceptionMessages, errorMessage)) { + avniError.userMessage = `${i18n.t(ErrorCodes[errorMessage] || errorMessage)}`; + avniError.showOnlyUserMessage = true; + } else { + avniError.userMessage = `${i18n.t(statusMessageKey)}. ${errorCode}. ${errorMessage}`; + } return ErrorUtil.getNavigableStackTraceSync(serverError); }).then((stackTraceString) => { avniError.reportingText = `${avniError.userMessage}\n\n${stackTraceString}`; diff --git a/packages/openchs-android/src/views/LoginView.js b/packages/openchs-android/src/views/LoginView.js index 4a00d5dbf..8cbbab736 100644 --- a/packages/openchs-android/src/views/LoginView.js +++ b/packages/openchs-android/src/views/LoginView.js @@ -158,35 +158,33 @@ class LoginView extends AbstractComponent { } displayFailureAlert(avniError, source) { - const isCatchmentError = avniError.userMessage.includes(ErrorCodes.NoCatchmentFound); - isCatchmentError ? this.noCatchmentAlert(this.I18n.t(ErrorCodes.NoCatchmentFound)) : - Alert.alert(this.I18n.t('restoreFailedTitle'), avniError.getDisplayMessage(), [ - { - text: this.I18n.t('tryAgain'), - onPress: () => this.dispatchAction(Actions.ON_DUMP_RESTORE_RETRY, { + Alert.alert(this.I18n.t('restoreFailedTitle'), avniError.getDisplayMessage(), [ + { + text: this.I18n.t('tryAgain'), + onPress: () => this.dispatchAction(Actions.ON_DUMP_RESTORE_RETRY, { + ...this.dumpRestoreAction.call(this), + source + }) + }, + { + text: "copyErrorTryAgain", + onPress: () => { + General.logDebug("LoginView", avniError.reportingText); + Clipboard.setString(avniError.reportingText); + ToastAndroid.show("reportCopiedReportByPasting", ToastAndroid.SHORT); + this.dispatchAction(Actions.ON_DUMP_RESTORE_RETRY, { ...this.dumpRestoreAction.call(this), source - }) - }, - { - text: "copyErrorTryAgain", - onPress: () => { - General.logDebug("LoginView", avniError.reportingText); - Clipboard.setString(avniError.reportingText); - ToastAndroid.show("reportCopiedReportByPasting", ToastAndroid.SHORT); - this.dispatchAction(Actions.ON_DUMP_RESTORE_RETRY, { - ...this.dumpRestoreAction.call(this), - source - }); - } - }, - { - text: this.I18n.t('performNormalSync'), - onPress: () => this.loginComplete(source), - style: 'cancel' + }); } - ] - ); + }, + { + text: this.I18n.t('performNormalSync'), + onPress: () => this.loginComplete(source), + style: 'cancel' + } + ] + ); } restoreFailureAlert(error, source) { @@ -198,15 +196,6 @@ class LoginView extends AbstractComponent { } } - noCatchmentAlert(errorMessage) { - Alert.alert(this.I18n.t('restoreFailedTitle'), errorMessage, [{ - text: this.I18n.t('ok'), - onPress: () => BackHandler.exitApp() - } - ] - ); - } - render() { General.logDebug('LoginView', 'render'); const {width, height} = Dimensions.get('window');