refactor(errors): refactor the way errors, offline, & queued ops are handled

This commit is contained in:
Tyler Renelle
2014-02-06 12:12:44 -07:00
parent e36d3264be
commit 106a7fa475
3 changed files with 31 additions and 12 deletions
+27 -11
View File
@@ -200,24 +200,40 @@ window.habitrpg = angular.module('habitrpg',
}
// Handle errors
var interceptor = ['$rootScope', '$q', function ($rootScope, $q) {
$httpProvider.responseInterceptors.push(['$rootScope', '$q', function ($rootScope, $q) {
function success(response) {
return response;
}
function error(response) {
//var status = response.status;
response.data = (response.data.err) ? response.data.err : response.data;
if (response.status == 0) response.data = window.env.t('serverUnreach');
if (response.status == 500) response.data += window.env.t('seeConsole');
debugger;
// Offline
if (response.status == 0 ||
// don't know why we're getting 404 here, should be 0
(response.status == 404 && _.isEmpty(response.data))) {
$rootScope.$broadcast('responseText', window.env.t('serverUnreach'));
var error = response.status == 0 ? response.data : (window.env.t('error') + ' ' + response.status + ': ' + response.data);
$rootScope.$broadcast('responseError', error);
console.log(arguments);
return $q.reject(response);
// Needs refresh
} else if (response.needRefresh) {
$rootScope.$broadcast('responseError', "The site has been updated and the page needs to refresh. The last action has not been recorded, please refresh and try again.");
// 400 range?
} else if (response < 500) {
$rootScope.$broadcast('responseText', response.data.err || response.data);
// Error
} else {
var error = '<strong>Please reload</strong>, ' +
'"'+window.env.t('error')+' '+(response.data.err || response.data || 'something went wrong')+'"' +
window.env.t('seeConsole');
$rootScope.$broadcast('responseError', error);
console.error(response);
}
//return $q.reject(response); // this completely halts the chain, meaning we can't queue offline actions
return response;
}
return function (promise) {
return promise.then(success, error);
}
}];
$httpProvider.responseInterceptors.push(interceptor);
}]);
}])
@@ -103,5 +103,8 @@ habitrpg.controller('NotificationCtrl',
$rootScope.$on('responseError', function(ev, error){
Notification.error(error);
});
$rootScope.$on('responseText', function(ev, error){
Notification.text(error);
});
}
]);
+1 -1
View File
@@ -10,7 +10,7 @@ angular.module("notificationServices", [])
top_offset: 20,
align: 'right', //('left', 'right', or 'center')
width: 250, //(integer, or 'auto')
delay: 7000,
delay: (type=='error') ? 0 : 7000,
allow_dismiss: true,
stackup_spacing: 10 // spacing between consecutive stacecked growls.
});