From 9b2549e543f6111316a0cdcc0db052174c7b2b86 Mon Sep 17 00:00:00 2001
From: "mathias.chouet" <mathias.chouet@irstea.fr>
Date: Fri, 17 May 2019 11:55:36 +0200
Subject: [PATCH] Fix #202

---
 src/app/app.component.ts                                | 7 ++++++-
 src/app/services/formulaire/formulaire.service.ts       | 5 +++--
 src/app/services/notifications/notifications.service.ts | 2 +-
 src/locale/messages.en.json                             | 1 +
 src/locale/messages.fr.json                             | 1 +
 5 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/src/app/app.component.ts b/src/app/app.component.ts
index 49857e238..abc820236 100644
--- a/src/app/app.component.ts
+++ b/src/app/app.component.ts
@@ -438,7 +438,12 @@ export class AppComponent implements OnInit, OnDestroy, Observer {
         if (result.emptySession) {
           this.doEmptySession();
         }
-        this.formulaireService.loadSession(result.file, result.calculators);
+        this.formulaireService.loadSession(result.file, result.calculators).catch((err) => {
+          this.notificationsService.notify(this.intlService.localizeText("ERROR_LOADING_SESSION"));
+          console.error("error loading session - ", err);
+          // rollback to ensure session is clean
+          this.doEmptySession();
+        });
       }
     });
   }
diff --git a/src/app/services/formulaire/formulaire.service.ts b/src/app/services/formulaire/formulaire.service.ts
index 655be967d..47e7941dc 100644
--- a/src/app/services/formulaire/formulaire.service.ts
+++ b/src/app/services/formulaire/formulaire.service.ts
@@ -454,8 +454,8 @@ export class FormulaireService extends Observable {
      * @param f fichier session
      * @param formInfos infos sur les modules de calcul @see DialogLoadSessionComponent.calculators
      */
-    public loadSession(f: File, formInfos: any[]) {
-        this.readSingleFile(f).then(s => {
+    public loadSession(f: File, formInfos: any[]): Promise<any> {
+        return this.readSingleFile(f).then(s => {
             const uids: string[] = [];
             formInfos.forEach((fi) => {
                 if (fi.selected) {
@@ -472,6 +472,7 @@ export class FormulaireService extends Observable {
                 this.createFormulaire(nn.nub.calcType, nn.nub, title);
             });
         }).catch(err => {
+            // forward errors to caller, to avoid "Uncaught (in promise)…"
             throw err;
         });
     }
diff --git a/src/app/services/notifications/notifications.service.ts b/src/app/services/notifications/notifications.service.ts
index 9caf0d7ff..bb404d860 100644
--- a/src/app/services/notifications/notifications.service.ts
+++ b/src/app/services/notifications/notifications.service.ts
@@ -20,7 +20,7 @@ export class NotificationsService {
     }
 
     /** Push a notification and display it as soon as possible */
-    public notify(message: string, duration: number, action: string = "OK") {
+    public notify(message: string, duration: number = 2000, action: string = "OK") {
         this.notifications.push({
             message: message,
             duration: duration,
diff --git a/src/locale/messages.en.json b/src/locale/messages.en.json
index 1f84ea625..ddccf44b3 100644
--- a/src/locale/messages.en.json
+++ b/src/locale/messages.en.json
@@ -10,6 +10,7 @@
     "ERROR_INTERVAL_OUTSIDE": "Interval: value %value% is outside of %interval",
     "ERROR_INTERVAL_UNDEF": "Interval: invalid 'undefined' value",
     "ERROR_INVALID_AT_POSITION": "Position %s:",
+    "ERROR_LOADING_SESSION": "Unable to load session",
     "ERROR_MINMAXSTEP_MAX": "Value is not in ]%s,%s]",
     "ERROR_MINMAXSTEP_MIN": "Value is not in [%s,%s[",
     "ERROR_MINMAXSTEP_STEP": "Value is not in %s",
diff --git a/src/locale/messages.fr.json b/src/locale/messages.fr.json
index 77620375a..6549da552 100644
--- a/src/locale/messages.fr.json
+++ b/src/locale/messages.fr.json
@@ -10,6 +10,7 @@
     "ERROR_INTERVAL_OUTSIDE": "Interval&nbsp;: la valeur %value% est hors de l'intervalle %interval",
     "ERROR_INTERVAL_UNDEF": "Interval&nbsp;: valeur 'undefined' incorrecte",
     "ERROR_INVALID_AT_POSITION": "Position %s :",
+    "ERROR_LOADING_SESSION": "Impossible de charger la session",
     "ERROR_MINMAXSTEP_MAX": "La valeur n'est pas dans ]%s,%s]",
     "ERROR_MINMAXSTEP_MIN": "La valeur n'est pas dans [%s,%s[",
     "ERROR_MINMAXSTEP_STEP": "La valeur n'est pas dans %s",
-- 
GitLab