Added a global error handler

This commit is contained in:
Siteshwar Vashisht 2013-10-19 22:36:18 +05:30
parent eb9e712f91
commit 892cc7da31
2 changed files with 37 additions and 1 deletions

View file

@ -94,7 +94,7 @@ function interpret_color(str) {
<body>
<div id="ancestor">
<span style="font-size: 16pt; color: #8888FF">fish</span><p id="global_error" class="error_msg"></p>
<span style="font-size: 16pt; color: #8888FF">fish</span><p id="global_error" class="error_msg" error-message></p>
<div id="parent">
<div id="tab_parent" ng-controller="main">
<div ng-class="tabCssClass('colors')" id="tab_colors" ng-click="changeView('colors')">colors</div>

View file

@ -31,3 +31,39 @@ fishconfig.config(
redirectTo: "/colors"
})
}]);
/* Inspired from http://blog.tomaka17.com/2012/12/random-tricks-when-using-angularjs/ */
fishconfig.config(function($httpProvider, $compileProvider) {
var global_error_element = null;
var showMessage = function(content) {
global_error_element.text(content);
};
$httpProvider.responseInterceptors.push(function($q) {
return function(promise) {
return promise.then(function(successResponse) {
showMessage('');
return successResponse;
}, function(errorResponse) {
switch (errorResponse.status) {
case 0:
showMessage("The request received an error. Perhaps the server has shut down.");
break;
case 500:
showMessage('Server internal error: ' + errorResponse.data);
break;
default:
showMessage('Error ' + errorResponse.status + ': ' + errorResponse.data);
}
return $q.reject(errorResponse);
});
};
});
$compileProvider.directive('errorMessage', function() {
return {
link: function(scope, element, attrs) { global_error_element = element; }
};
});
});