Skip to content

Commit

Permalink
#1475 | Friendly error message on /idp-details call when server is down
Browse files Browse the repository at this point in the history
  • Loading branch information
1t5j0y committed Oct 29, 2024
1 parent b20b1af commit c13dfe4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
4 changes: 3 additions & 1 deletion packages/openchs-android/src/views/LoginView.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {EntityMappingConfig} from "openchs-models";
import EntityService from "../service/EntityService";
import ServerError, {getAvniError} from "../service/ServerError";
import ErrorUtil from "../framework/errorHandling/ErrorUtil";
import { AlertMessage } from "./common/AlertMessage";

@Path('/loginView')
class LoginView extends AbstractComponent {
Expand Down Expand Up @@ -349,8 +350,9 @@ class LoginView extends AbstractComponent {
this.getService(AuthService).getAuthProviderService().logout()
.then(() => this.getService(SyncService).clearData())
.then(() => this.getService(AuthService).fetchAuthSettingsFromServer())
.catch(error => getAvniError(error, this.i18n).then(avniError => AlertMessage(this.i18n.t('Error'), avniError.getDisplayMessage())))
.then(() => this.reset())
.then(() => this.justLogin());
.then(() => this.justLogin())
}

safeLogin() {
Expand Down
13 changes: 10 additions & 3 deletions packages/openchs-android/src/views/MenuView.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ import StaticMenuItem from "./menu/StaticMenuItem";
import AvniIcon from "./common/AvniIcon";
import EntityService from "../service/EntityService";
import EnvironmentConfig from "../framework/EnvironmentConfig";
import { getAvniError } from "../service/ServerError";
import { AlertMessage } from "./common/AlertMessage";
import MessageService from "../service/MessageService";

@Path('/menuView')
class MenuView extends AbstractComponent {
Expand Down Expand Up @@ -98,10 +101,14 @@ class MenuView extends AbstractComponent {
const authService = this.context.getService(AuthService);
authService.getAuthProviderService().logout()
.then(() => authService.fetchAuthSettingsFromServer())
.catch((error) => {
const i18n = this.getService(MessageService).getI18n();
getAvniError(error, i18n).then(avniError => AlertMessage(i18n.t('Error'), avniError.getDisplayMessage()));
})
.then(() => {
logEvent(firebaseEvents.LOG_OUT);
CHSNavigator.navigateToLoginView(this, false);
});
logEvent(firebaseEvents.LOG_OUT);
CHSNavigator.navigateToLoginView(this, false);
})
};

logout() {
Expand Down
13 changes: 11 additions & 2 deletions packages/openchs-android/src/views/RootView.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import React from "react";
import AbstractComponent from "../framework/view/AbstractComponent";
import Path, {PathRoot} from "../framework/routing/Path";
import Path, { PathRoot } from "../framework/routing/Path";
import AuthService from "../service/AuthService";
import CHSNavigator from "../utility/CHSNavigator";
import BeneficiaryModePinService from "../service/BeneficiaryModePinService";
import BackupRestoreRealmService from "../service/BackupRestoreRealm";
import General from "../utility/General";
import HomeScreenView from "./HomeScreenView";
import ExtensionService from "../service/ExtensionService";
import { getAvniError } from "../service/ServerError";
import MessageService from "../service/MessageService";
import { AlertMessage } from "./common/AlertMessage";
import { BackHandler } from "react-native";

@Path('/rootView')
@PathRoot
Expand Down Expand Up @@ -40,7 +44,12 @@ class RootView extends AbstractComponent {
async openApp() {
const authService = await this.context.getService(AuthService);
if (! await authService.isAuthInitialized()) {
await authService.fetchAuthSettingsFromServer();
try {
await authService.fetchAuthSettingsFromServer();
} catch (error) {
const i18n = this.getService(MessageService).getI18n();
getAvniError(error, i18n).then(avniError => AlertMessage(i18n.t('Error'), avniError.getDisplayMessage(), BackHandler.exitApp));
}
}
const decisionParameters = await this.nextScreenDecisionParameters();

Expand Down
4 changes: 2 additions & 2 deletions packages/openchs-android/src/views/common/AlertMessage.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {Alert} from "react-native";
import _ from 'lodash';

export const AlertMessage = (title, message) => {
export const AlertMessage = (title, message, onPress = _.noop) => {
const displayMessage = typeof message === "string" ? message : JSON.stringify(message);
Alert.alert(
title,
displayMessage,
[
{text: 'OK', onPress: _.noop}
{text: 'OK', onPress: onPress}
],
{cancelable: false}
);
Expand Down

0 comments on commit c13dfe4

Please sign in to comment.