diff --git a/assets/js/app.js b/assets/js/app.js index e9be296c93..7a00d04f36 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -23,4 +23,24 @@ window.habitrpg = angular.module('habitrpg', $httpProvider.defaults.headers.common['x-api-user'] = settings.auth.apiId; $httpProvider.defaults.headers.common['x-api-key'] = settings.auth.apiId; } + + // Handle errors + var interceptor = ['$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 = 'Server currently unreachable'; + if (response.status == 500) response.data += '(see Chrome console for more details).'; + $rootScope.flash.errors.push(response.status + ': ' + response.data); + console.log(arguments); + return $q.reject(response); + } + return function (promise) { + return promise.then(success, error); + } + }]; + $httpProvider.responseInterceptors.push(interceptor); }]) \ No newline at end of file diff --git a/assets/js/controllers/rootCtrl.js b/assets/js/controllers/rootCtrl.js index e261c606b3..1f231d7ad3 100644 --- a/assets/js/controllers/rootCtrl.js +++ b/assets/js/controllers/rootCtrl.js @@ -9,6 +9,7 @@ habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$ $rootScope.User = User; $rootScope.user = User.user; $rootScope.settings = User.settings; + $rootScope.flash = {errors: [], warnings: []}; /* FIXME this is dangerous, organize helpers.coffee better, so we can group them by which controller needs them, diff --git a/views/index.jade b/views/index.jade index 06fc9c2007..20c76129c5 100644 --- a/views/index.jade +++ b/views/index.jade @@ -84,8 +84,8 @@ html #wrap // Errors - ul.unstyled.alert.alert-error(ng-show='_flash.error') - li(ng-repeat='error in _flash.error') {{error}} + .unstyled.alert.alert-error(ng-repeat='error in flash.errors') {{error}} + .unstyled.alert.alert-error(ng-repeat='error in flash.warnings') {{error}} //if they hide the header, we still need user-menu visible div(ng-if='user.preferences.hideHeader')