From 37425d061fc3dd72da7eb91245d0c6c9f00109c1 Mon Sep 17 00:00:00 2001
From: "mathias.chouet" <mathias.chouet@irstea.fr>
Date: Tue, 21 May 2019 15:34:42 +0200
Subject: [PATCH] Chargement de session: avertissement en cas d'erreur non
 fatale

---
 src/app/app.component.ts                       | 18 ++++++++++++------
 .../services/formulaire/formulaire.service.ts  | 12 ++++++++----
 src/locale/messages.en.json                    |  1 +
 src/locale/messages.fr.json                    |  1 +
 4 files changed, 22 insertions(+), 10 deletions(-)

diff --git a/src/app/app.component.ts b/src/app/app.component.ts
index ae8842a56..c4ebf521a 100644
--- a/src/app/app.component.ts
+++ b/src/app/app.component.ts
@@ -466,12 +466,18 @@ export class AppComponent implements OnInit, OnDestroy, Observer {
         if (result.emptySession) {
           this.doEmptySession();
         }
-        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();
-        });
+        this.formulaireService.loadSession(result.file, result.calculators)
+          .then((data) => {
+            if (data.hasErrors) {
+              this.notificationsService.notify(this.intlService.localizeText("ERROR_PROBLEM_LOADING_SESSION"), 3500);
+            }
+          })
+          .catch((err) => {
+            this.notificationsService.notify(this.intlService.localizeText("ERROR_LOADING_SESSION"), 3500);
+            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 642f163ad..116bf898f 100644
--- a/src/app/services/formulaire/formulaire.service.ts
+++ b/src/app/services/formulaire/formulaire.service.ts
@@ -513,7 +513,7 @@ export class FormulaireService extends Observable {
      * @param f fichier session
      * @param formInfos infos sur les modules de calcul @see DialogLoadSessionComponent.calculators
      */
-    public async loadSession(f: File, formInfos: any[]): Promise<any> {
+    public async loadSession(f: File, formInfos: any[]): Promise<{ hasErrors: boolean }> {
         try {
             const s = await this.readSingleFile(f);
             const uids: string[] = [];
@@ -522,7 +522,8 @@ export class FormulaireService extends Observable {
                     uids.push(fi.uid);
                 }
             });
-            const newNubs = Session.getInstance().unserialise(s, uids);
+            const res = Session.getInstance().unserialise(s, uids);
+            const newNubs = res.nubs;
             // for each new Nub, create the related form, set its title
             for (let i = 0; i < newNubs.length; i++) {
                 const nn = newNubs[i];
@@ -534,10 +535,13 @@ export class FormulaireService extends Observable {
                 await this.loadLocalisation(nn.nub.calcType);
                 await this.createFormulaire(nn.nub.calcType, nn.nub, title); // await guarantees loading order
             }
-            return s;
+            // forward errors
+            return {
+                hasErrors: res.hasErrors
+            };
 
         } catch (err) {
-            // forward errors to caller, to avoid "Uncaught (in promise)…"
+            // forward errors to caller to avoid "Uncaught (in promise)"
             throw err;
         }
     }
diff --git a/src/locale/messages.en.json b/src/locale/messages.en.json
index 2b6aa4f81..2f56f60c7 100644
--- a/src/locale/messages.en.json
+++ b/src/locale/messages.en.json
@@ -29,6 +29,7 @@
     "ERROR_PARAMDEF_VALUE_UNDEFINED": "value of '%symbol%' parameter is undefined",
     "ERROR_PARAMDOMAIN_INTERVAL_BOUNDS": "invalid %minValue%/%maxValue% min/max boundaries for 'interval' parameter definition domain",
     "ERROR_PARAMDOMAIN_INVALID": "parameter '%symbol%: non supported '%domain%' definition domain",
+    "ERROR_PROBLEM_LOADING_SESSION": "Session loaded, with errors",
     "ERROR_REMOUS_PAS_CALCUL_DEPUIS_AMONT": "Upstream boundary condition < Critical elevation: no possible calculation from upstream",
     "ERROR_REMOUS_PAS_CALCUL_DEPUIS_AVAL": "Downstream boundary condition < Critical elevation: no possible calculation from downstream",
     "ERROR_REMOUS_PENTE_FORTE": "The water line slope is too steep at abscissa %x% m (the discretisation step should be reduced)",
diff --git a/src/locale/messages.fr.json b/src/locale/messages.fr.json
index ba8010d4d..d18bb02e5 100644
--- a/src/locale/messages.fr.json
+++ b/src/locale/messages.fr.json
@@ -29,6 +29,7 @@
     "ERROR_PARAMDEF_VALUE_UNDEFINED": "La valeur du paramètre %symbol% n'est pas définie",
     "ERROR_PARAMDOMAIN_INTERVAL_BOUNDS": "Les bornes (%minValue%/%maxValue%) de l'intervalle sont incorrectes",
     "ERROR_PARAMDOMAIN_INVALID": "Paramètre '%symbol%'&nbsp;: le domaine de définition '%domain%' est incorrect",
+    "ERROR_PROBLEM_LOADING_SESSION": "Session chargée, avec des erreurs",
     "ERROR_REMOUS_PAS_CALCUL_DEPUIS_AMONT": "Condition limite amont > Hauteur critique&nbsp;: pas de calcul possible depuis l'amont",
     "ERROR_REMOUS_PAS_CALCUL_DEPUIS_AVAL": "Condition limite aval < Hauteur critique&nbsp;: pas de calcul possible depuis l'aval",
     "ERROR_REMOUS_PENTE_FORTE": "La pente de la ligne d'eau est trop forte à l'abscisse %x% m (il faudrait réduire le pas de discrétisation)",
-- 
GitLab