-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobalErrors.js
More file actions
55 lines (51 loc) · 1.63 KB
/
globalErrors.js
File metadata and controls
55 lines (51 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'use strict';
angular.module('GlobalErrors', [])
.factory('GlobalErrors', function(notificationFactory) {
var errors = {
0: function(errorResponse) {
notificationFactory.error(0, 'Error ' + errorResponse.status + ': ' + errorResponse.data);
},
401: function() {
notificationFactory.error(401, 'Uh oh.. You are not logged in. Please log in again. Thank you.');
},
403: function() {
notificationFactory.error(403, 'Sorry, you have not enough cats to view this');
},
500: function(errorResponse) {
notificationFactory.error(500, 'Server internal error: ' + errorResponse.data);
}
};
return {
set: function(code, callback) {
errors[code] = callback;
},
run: function(code, response) {
if(errors[code] === undefined)
{
code = 0;
}
if(errors[code] !== undefined && typeof(errors[code]) === 'function')
{
errors[code](response);
}
}
};
})
.config(function ($provide, $httpProvider, $compileProvider) {
var elementsList = $();
$httpProvider.responseInterceptors.push(function ($timeout, $q, notificationFactory, AppLoader, GlobalErrors) {
return function (promise) {
AppLoader.show();
return promise.then(function (successResponse) {
// Don't push the notification on success here, let the controller decide to push the notification
// The controller needs a callback function we set in the controller and run here..
AppLoader.hide();
return successResponse;
}, function (errorResponse) {
GlobalErrors.run(errorResponse.status, errorResponse);
AppLoader.hide();
return $q.reject(errorResponse);
});
};
});
});