瀏覽代碼

fix: catch error in Promise

jmacura 5 年之前
父節點
當前提交
5425d23ed1
共有 1 個文件被更改,包括 53 次插入38 次删除
  1. 53 38
      src/adjuster/adjuster.service.js

+ 53 - 38
src/adjuster/adjuster.service.js

@@ -40,9 +40,10 @@ export class AdjusterService {
             };
           }),
         },*/
-      }).then((response) => {
-        const clusterData = response.data.response;
-        /*let max = 0;
+      })
+        .then((response) => {
+          const clusterData = response.data.response;
+          /*let max = 0;
         this.clusters.forEach((a) => {
           if (a.aggregate > max) {
             max = a.aggregate;
@@ -55,27 +56,33 @@ export class AdjusterService {
         this.attractivity.forEach((a) => {
           this.nutsCodeRecordRelations[a.code] = a;
         });*/
-        nuts.nuts3Source.forEachFeature((feature) => {
-          // Pair each feature with its clustering data
-          const featureData = clusterData.find(
-            (item) => item['nuts_id'] === feature.get('NUTS_ID')
-          );
-          Object.keys(featureData).forEach(function (key, index) {
-            if (key !== 'nuts_id') {
-              feature.set(key, featureData[key]);
-            }
+          nuts.nuts3Source.forEachFeature((feature) => {
+            // Pair each feature with its clustering data
+            const featureData = clusterData.find(
+              (item) => item['nuts_id'] === feature.get('NUTS_ID')
+            );
+            Object.keys(featureData).forEach(function (key, index) {
+              if (key !== 'nuts_id') {
+                feature.set(key, featureData[key]);
+              }
+            });
           });
-        });
-        const clusters = [];
-        for (const region of clusterData) {
-          if (!clusters.includes(region['km25.cluster'])) {
-            clusters.push(region['km25.cluster']);
+          const clusters = [];
+          for (const region of clusterData) {
+            if (!clusters.includes(region['km25.cluster'])) {
+              clusters.push(region['km25.cluster']);
+            }
           }
-        }
-        this.clusters = clusters;
-        this._clusteringInProcess = false;
-        this.$rootScope.$broadcast('clusters_loaded');
-      });
+          this.clusters = clusters;
+          this._clusteringInProcess = false;
+          this.$rootScope.$broadcast('clusters_loaded');
+        })
+        .catch((error) => {
+          console.error(
+            `Error obtaining data from ${this.serviceBaseUrl}: ${error}`
+          );
+          this._clusteringInProcess = false;
+        });
     };
     this.HsUtilsService.debounce(f, 300)();
   }
@@ -83,23 +90,31 @@ export class AdjusterService {
   init() {
     this.$http({
       url: this.serviceBaseUrl + 'datasets',
-    }).then((response) => {
-      this.factors = response.data.map((dataset) => {
-        return {name: dataset.Factor, weight: 1, datasets: []};
-      });
-      this.factors = this.HsUtilsService.removeDuplicates(this.factors, 'name');
-      this.factors.forEach((factor) => {
-        factor.datasets = response.data
-          .filter((ds) => ds.Factor === factor.name)
-          .map((ds) => {
-            return {
-              name: ds.Name,
-              included: true,
-            };
-          });
+    })
+      .then((response) => {
+        this.factors = response.data.map((dataset) => {
+          return {name: dataset.Factor, weight: 1, datasets: []};
+        });
+        this.factors = this.HsUtilsService.removeDuplicates(
+          this.factors,
+          'name'
+        );
+        this.factors.forEach((factor) => {
+          factor.datasets = response.data
+            .filter((ds) => ds.Factor === factor.name)
+            .map((ds) => {
+              return {
+                name: ds.Name,
+                included: true,
+              };
+            });
+        });
+        this.apply();
+      })
+      .catch((error) => {
+        console.error(`Web service at ${this.serviceBaseUrl} unavailable!`);
+        this._clusteringInProcess = false;
       });
-      this.apply();
-    });
   }
 
   /**