Skip to content

Commit

Permalink
More fetch error reporting + json clone toHawkejs usage
Browse files Browse the repository at this point in the history
  • Loading branch information
skerit committed Mar 10, 2016
1 parent 91755f7 commit 572bde2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
36 changes: 32 additions & 4 deletions lib/class/scene.js
Expand Up @@ -643,15 +643,29 @@ module.exports = function hawkejsSceneClass(Hawkejs, Blast) {

// Catch errors
xhr.addEventListener('error', function transferFailed(e) {

var err;

if (callback) {
callback(new Error('Transfer failed'));
err = new Error('Transfer failed');

// Simulate a 408 "timeout"
err.status = err.number = 408;

callback(err, null, xhr);
}
}, false);

// Catch aborts
xhr.addEventListener('abort', function transferCanceled(e) {

var err;

if (callback) {
callback(new Error('Transfer aborted'));
err = new Error('Transfer aborted');
err.status = err.number = 0;

callback(err, null, xhr);
}
}, false);

Expand All @@ -665,6 +679,18 @@ module.exports = function hawkejsSceneClass(Hawkejs, Blast) {

response = xhr.response || xhr.responseText;

// Callback through this function when response is received
function doCallback() {
var err = null;

if (xhr.status > 399) {
err = new Error(xhr.statusText);
err.status = err.number = xhr.status;
}

callback(err, result, xhr);
}

// Check for FileReader / Blob supported browsers
if (typeof FileReader !== 'undefined') {
reader = new FileReader();
Expand All @@ -677,7 +703,7 @@ module.exports = function hawkejsSceneClass(Hawkejs, Blast) {
result = Blast.Collection.JSON.undry(result);
}

callback(null, result, xhr);
doCallback();
};

reader.readAsText(xhr.response);
Expand All @@ -689,7 +715,7 @@ module.exports = function hawkejsSceneClass(Hawkejs, Blast) {
result = Blast.Collection.JSON.undry(response);
}

callback(null, result, xhr);
doCallback();
}
}, false);

Expand Down Expand Up @@ -2469,6 +2495,8 @@ module.exports = function hawkejsSceneClass(Hawkejs, Blast) {

// Request the initial check
req();

return req;
});

// To get a prettier output in the browser console
Expand Down
4 changes: 2 additions & 2 deletions lib/class/view_render.js
Expand Up @@ -619,8 +619,8 @@ module.exports = function(Hawkejs, Blast) {
return;
}

// Clone the variables
this.variables = Blast.Collection.JSON.clone(this.variables);
// Clone the variables, use the toHawkejs method if available
this.variables = Blast.Collection.JSON.clone(this.variables, 'toHawkejs');

// Initialize all the helpers
this.initHelpers();
Expand Down

0 comments on commit 572bde2

Please sign in to comment.