Improve handling of unexpected package download/upload request errors
[osm/UI.git] / skyquake / plugins / composer / src / src / stores / CatalogPackageManagerStore.js
index 22d6359..5ffe83f 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * 
+ *
  *   Copyright 2016 RIFT.IO Inc
  *
  *   Licensed under the Apache License, Version 2.0 (the "License");
@@ -18,7 +18,8 @@
  */
 'use strict';
 
-import _ from 'lodash'
+import _delay from 'lodash/delay'
+import _pick from 'lodash/pick'
 import alt from '../alt'
 import guid from '../libraries/guid'
 import numeral from 'numeral'
@@ -65,7 +66,7 @@ function delayStatusCheck(statusCheckFunction, catalogPackage) {
                        delete catalogPackage.checkStatusTimeoutId;
                        statusCheckFunction(catalogPackage).catch(exception);
                };
-               catalogPackage.checkStatusTimeoutId = _.delay(delayCallback, defaults.checkStatusDelayInSeconds * 1000);
+               catalogPackage.checkStatusTimeoutId = _delay(delayCallback, defaults.checkStatusDelayInSeconds * 1000);
        }
 }
 
@@ -109,7 +110,7 @@ class CatalogPackageManagerStore {
 
        uploadCatalogPackage(file) {
                file.id = file.id || guid();
-               const catalogPackage = _.pick(file, packagePropertyNames);
+               const catalogPackage = _pick(file, packagePropertyNames);
                catalogPackage.icon = file.riftAction === 'onboard' ? imgOnboard : imgUpdate;
                catalogPackage.type = 'upload';
                this.addPackage(catalogPackage);
@@ -119,12 +120,15 @@ class CatalogPackageManagerStore {
        onUploadCatalogPackageStatusUpdated(response) {
                const upload = updateStatusInfo(response);
                this.updatePackage(upload);
+               console.log('updating package upload')
                // if pending with no transaction id - do nothing
                // bc DropZone.js will notify upload progress
                if (upload.pending && upload.transactionId) {
+                       console.log('checking status')
                        delayStatusCheck(this.getInstance().requestCatalogPackageUploadStatus, upload);
                } else if (upload.success) {
                        this.getInstance().loadCatalogs();
+                       console.log('finished uploading to node, requesting status from rest')
                }
        }
 
@@ -192,51 +196,57 @@ function updateStatusInfo(response) {
        };
        const responseData = (response.data.output) ? response.data.output :  response.data;
        const catalogPackage = response.state;
-       switch(response.data.status) {
-       case 'upload-progress':
-               statusInfo.pending = true;
-               statusInfo.progress = parseFloat(responseData.progress) || 0;
-               statusInfo.message = calculateUploadProgressMessage(catalogPackage.size, responseData.progress, responseData.bytesSent);
-               break;
-       case 'upload-success':
-               statusInfo.pending = true;
-               statusInfo.progress = 100;
-               statusInfo.message = 'Upload completed.';
-               statusInfo.transactionId = responseData['transaction-id'] || catalogPackage.transactionId;
-               break;
-       case 'upload-error':
-               statusInfo.error = true;
-               statusInfo.message = responseData.message;
-               break;
-       case 'download-requested':
-               statusInfo.pending = true;
-               statusInfo.progress = 25;
-               statusInfo.transactionId = responseData['transaction-id']  || catalogPackage.transactionId;
-               break;
-       case 'pending':
-               statusInfo.pending = true;
-               statusInfo.progress = 50;
-               statusInfo.message = responseData.events[responseData.events.length - 1].text;
-               break;
-       case 'success':
-               statusInfo.success = true;
-               statusInfo.progress = 100;
-               statusInfo.message = responseData.events[responseData.events.length - 1].text;
-               if (catalogPackage.type === 'download') {
-                       statusInfo.urlValidUntil = moment().add(defaults.downloadUrlTimeToLiveInMinutes, 'minutes').toISOString();
-                       if (responseData.filename) {
-                               statusInfo.url = getCatalogPackageManagerServerOrigin() + '/api/export/' + responseData.filename;
-                       } else {
-                               statusInfo.url = getCatalogPackageManagerServerOrigin() + '/api/export/' + catalogPackage.transactionId + '.tar.gz';
+       if ( typeof response.data.status !== "number" ) {
+               switch(response.data.status) {
+               case 'upload-progress':
+                       statusInfo.pending = true;
+                       statusInfo.progress = parseFloat(responseData.progress) || 0;
+                       statusInfo.message = calculateUploadProgressMessage(catalogPackage.size, responseData.progress, responseData.bytesSent);
+                       break;
+               case 'upload-success':
+                       statusInfo.pending = true;
+                       statusInfo.progress = 100;
+                       statusInfo.message = 'Upload completed.';
+                       statusInfo.transactionId = responseData['transaction_id'] || responseData['transaction-id'] || catalogPackage.transactionId;
+                       break;
+               case 'upload-error':
+                       statusInfo.error = true;
+                       statusInfo.message = responseData.message;
+                       break;
+               case 'download-requested':
+                       statusInfo.pending = true;
+                       statusInfo.progress = 25;
+                       statusInfo.transactionId = responseData['transaction_id'] || responseData['transaction-id']  || catalogPackage.transactionId;
+                       break;
+               case 'pending':
+                       statusInfo.pending = true;
+                       statusInfo.progress = 50;
+                       statusInfo.message = responseData.events[responseData.events.length - 1].text;
+                       break;
+               case 'success':
+                       statusInfo.success = true;
+                       statusInfo.progress = 100;
+                       statusInfo.message = responseData.events[responseData.events.length - 1].text;
+                       if (catalogPackage.type === 'download') {
+                               statusInfo.urlValidUntil = moment().add(defaults.downloadUrlTimeToLiveInMinutes, 'minutes').toISOString();
+                               if (responseData.filename) {
+                                       statusInfo.url = getCatalogPackageManagerServerOrigin() + '/api/export/' + responseData.filename;
+                               } else {
+                                       statusInfo.url = getCatalogPackageManagerServerOrigin() + '/api/export/' + catalogPackage.transactionId + '.tar.gz';
+                               }
                        }
+                       break;
+               case 'failure':
+                       statusInfo.error = true;
+                       statusInfo.message = responseData.errors[0].value;
+                       break;
+               default:
+                       throw new ReferenceError('a status of "request", "success", "failure", "pending", "upload-completed", "upload-error", "download-requested", "upload-progress", "upload-action" is required');
                }
-               break;
-       case 'failure':
+       } else {
+               // typically get here due to unexpected development errors (backend exceptions, down/up load server access issues)
                statusInfo.error = true;
-               statusInfo.message = responseData.errors[0].value;
-               break;
-       default:
-               throw new ReferenceError('a status of "request", "success", "failure", "pending", "upload-completed", "upload-error", "download-requested", "upload-progress", "upload-action" is required');
+               statusInfo.message = responseData.statusText || 'Error';
        }
        return Object.assign({}, catalogPackage, statusInfo);
 }