Skip to content

Commit

Permalink
feat(uploads): capture error message (#6)
Browse files Browse the repository at this point in the history
* feat(uploads): capture error message
* chore(uploads): clean up upload properties
* chore(upload-core): lint fix
* feat(upload-core): add error message
  • Loading branch information
christieb7007 authored and robmcguinness committed Feb 21, 2018
1 parent df77ca1 commit 5af448e
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions packages/upload-core/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ class Upload {

this.file = file;
this.options = Object.assign(options, defaults);
this.completed = false;
this.percentage = 0;
this.onError = [];
this.onSuccess = [];
this.onProgress = [];
this.bytesTotal = 0;
this.bytesSent = 0;
this.bytesScanned = 0;
this.errorMessage = null;
this.status = 'pending';
this.timeoutID = undefined;
}

Expand All @@ -51,6 +52,8 @@ class Upload {

xhr.onload = () => {
if (!this.inStatusCategory(xhr.status, 200)) {
this.status = 'rejected';
this.errorMessage = `Invalid status returned: ${xhr.status}`;
this.onError.forEach(cb => cb(xhr));
return;
}
Expand All @@ -62,8 +65,8 @@ class Upload {
this.onProgress.forEach(cb => cb());

if (result === 'accepted') {
this.completed = true;
this.percentage = 100;
this.status = result;
const references = xhr.getResponseHeader('references');
if (references) {
this.references = JSON.parse(references);
Expand All @@ -74,7 +77,9 @@ class Upload {

if (result === 'rejected') {
clearTimeout(this.timeoutId);
this.onError.forEach(cb => cb(new Error('File upload rejected')));
this.errorMessage = 'Failed Virus Scan';
this.status = result;
this.onError.forEach(cb => cb(new Error('Failed Virus Scan')));
return;
}

Expand All @@ -84,6 +89,9 @@ class Upload {
};

xhr.onerror = err => {
this.error = err;
this.status = 'rejected';
this.errorMessage = 'Network Error';
this.onError.forEach(cb => cb(err));
};

Expand Down Expand Up @@ -119,6 +127,7 @@ class Upload {
},
onError: err => {
this.error = err;
this.errorMessage = 'Network Error';
this.onError.forEach(cb => cb(err));
},
onProgress: (bytesSent, bytesTotal) => {
Expand All @@ -135,8 +144,8 @@ class Upload {
this.percentage = this.getPercentage();

if (result === 'accepted') {
this.completed = true;
this.percentage = 100;
this.status = result;
const references = xhr.getResponseHeader('references');
if (references) {
this.references = JSON.parse(references);
Expand All @@ -146,6 +155,8 @@ class Upload {
}

if (result === 'rejected') {
this.status = result;
this.errorMessage = 'File upload rejected';
this.onError.forEach(cb => cb(new Error('File upload rejected')));
return;
}
Expand Down

0 comments on commit 5af448e

Please sign in to comment.