Skip to content

Commit

Permalink
#1460 | Catchment not setup for user - error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
1t5j0y committed Aug 28, 2024
1 parent d66b2bd commit b349e45
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
15 changes: 13 additions & 2 deletions packages/openchs-android/src/service/ServerError.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -36,15 +37,25 @@ function getServerStatusMessageKey(serverError) {
}
}

const knownServerStatusMessages = ['serverUnavailableTryLater'];
const knownServerExceptionMessages = ['NoCatchmentFound'];

export function getAvniError(serverError, i18n) {
const statusCode = getStatusCode(serverError);
const errorCode = _.isNil(statusCode) ? "" : `Http ${statusCode}`;
const avniError = new AvniError();
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}`;
Expand Down
59 changes: 24 additions & 35 deletions packages/openchs-android/src/views/LoginView.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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');
Expand Down

0 comments on commit b349e45

Please sign in to comment.