1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00
unleash.unleash/public/js/stores/ErrorStore.js

69 lines
1.9 KiB
JavaScript
Raw Normal View History

2015-03-17 20:29:03 +01:00
var Reflux = require('reflux');
var FeatureActions = require('./FeatureToggleActions');
var ErrorActions = require('./ErrorActions');
// Creates a DataStore
var FeatureStore = Reflux.createStore({
// Initial setup
init: function() {
this.listenTo(FeatureActions.create.failed, this.onError);
2015-03-17 22:56:26 +01:00
this.listenTo(FeatureActions.init.failed, this.onError);
2015-03-17 20:29:03 +01:00
this.listenTo(FeatureActions.update.failed, this.onError);
this.listenTo(FeatureActions.archive.failed, this.onError);
this.listenTo(FeatureActions.revive.failed, this.onError);
this.listenTo(ErrorActions.error, this.onError);
this.listenTo(ErrorActions.clear, this.onClear);
this.errors = [];
},
onError: function (error) {
2015-03-17 20:29:03 +01:00
if (this.isClientError(error)) {
var errors = JSON.parse(error.responseText);
errors.forEach(function(e) {
this.addError(e.msg);
}.bind(this));
2015-03-17 20:29:03 +01:00
} else if (error.status === 0) {
this.addError("server unreachable");
} else {
this.addError(error);
}
},
onClear: function() {
this.errors = [];
this.trigger([]);
},
addError: function(msg) {
var errors = this.errors;
if (errors[errors.length - 1] !== msg) {
errors.push(msg);
this.errors = errors;
this.trigger(errors);
}
},
isClientError: function(error) {
try {
return error.status >= 400 &&
error.status < 500 &&
JSON.parse(error.responseText);
} catch (e) {
if (e instanceof SyntaxError) {
// fall through;
console.log("Syntax error!");
} else {
throw e;
}
}
return false;
},
getErrors: function() {
return this.errors;
}
});
module.exports = FeatureStore;