From 7db097372e6e2fc8e746c5a2572425a46fc3a0c6 Mon Sep 17 00:00:00 2001
From: FORESTIER Fabien <fabien.forestier@soprasteria.com>
Date: Wed, 29 Apr 2020 15:57:52 +0200
Subject: [PATCH] Improve error handling on download button

---
 .../resource-download-item.component.ts       | 18 ++++++----
 .../download-button.component.ts              | 35 +++++++++----------
 2 files changed, 29 insertions(+), 24 deletions(-)

diff --git a/src/app/dataset-detail/components/dataset-downloads/resource-download-item/resource-download-item/resource-download-item.component.ts b/src/app/dataset-detail/components/dataset-downloads/resource-download-item/resource-download-item/resource-download-item.component.ts
index e2787c7e..63420e73 100644
--- a/src/app/dataset-detail/components/dataset-downloads/resource-download-item/resource-download-item/resource-download-item.component.ts
+++ b/src/app/dataset-detail/components/dataset-downloads/resource-download-item/resource-download-item/resource-download-item.component.ts
@@ -262,13 +262,19 @@ export class ResourceDownloadItemComponent implements OnInit {
 
   transformObjectToArray(object) {
     const array = [];
-    const keys = Object.keys(object);
-    keys.forEach((key) => {
-      array.push({
-        name: key,
-        bbox: object[key],
+    let keys;
+
+    if (object) {
+      keys = Object.keys(object);
+
+      keys.forEach((key) => {
+        array.push({
+          name: key,
+          bbox: object[key],
+        });
       });
-    });
+    }
+
     return array;
   }
 
diff --git a/src/app/shared/components/download-button/download-button.component.ts b/src/app/shared/components/download-button/download-button.component.ts
index 790b624e..0672bce3 100644
--- a/src/app/shared/components/download-button/download-button.component.ts
+++ b/src/app/shared/components/download-button/download-button.component.ts
@@ -27,7 +27,7 @@ export class DownloadButtonComponent implements OnInit, OnDestroy {
     private _http: HttpClient,
   ) { }
 
-  ngOnInit() {}
+  ngOnInit() { }
 
   download() {
     this.loading = true;
@@ -35,7 +35,6 @@ export class DownloadButtonComponent implements OnInit, OnDestroy {
       takeUntil(this.ngUnsubscribe),
     ).subscribe(
       (response) => {
-        console.log('Reponse');
         // Create a temporary link and click on it to launch the blob download
         const url = window.URL.createObjectURL(response.body);
         const a = document.createElement('a');
@@ -50,23 +49,23 @@ export class DownloadButtonComponent implements OnInit, OnDestroy {
       (err) => {
         let message = notificationMessages.general.failedDownloadFile;
 
-        if (err && err.status) {
-          switch (err.status) {
-            case 401:
-              message = notificationMessages.general.failedDownloadFileUnauthenticated;
-              break;
-            case 403:
-              message = notificationMessages.general.failedDownloadFileUnauthorized;
-              break;
-          }
-          this.abortDownload();
-          this._notificationService.notify(
-            {
-              message,
-              type: 'error',
-            },
-          );
+        if (err && err.status === 401) {
+
+          message = notificationMessages.general.failedDownloadFileUnauthenticated;
+        }
+
+        if (err && err.status === 403) {
+          message = notificationMessages.general.failedDownloadFileUnauthorized;
         }
+
+        this._notificationService.notify(
+          {
+            message,
+            type: 'error',
+          },
+        );
+
+        this.abortDownload();
       },
     );
   }
-- 
GitLab