-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathgeneric-error-handler.js
112 lines (96 loc) · 4.55 KB
/
generic-error-handler.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var HEADER_NAME = 'NzbHydra2-Handle-Errors-Generically';
var specificallyHandleInProgress = false;
nzbhydraapp.factory('RequestsErrorHandler', function ($q, growl, blockUI, GeneralModalService) {
return {
// --- The user's API for claiming responsiblity for requests ---
specificallyHandled: function (specificallyHandledBlock) {
specificallyHandleInProgress = true;
try {
return specificallyHandledBlock();
} finally {
specificallyHandleInProgress = false;
}
},
// --- Response interceptor for handling errors generically ---
responseError: function (rejection) {
blockUI.reset();
if (rejection.data instanceof ArrayBuffer) {
//The case when the response was specifically requested as that, e.g. for debug infos
rejection.data = JSON.parse(new TextDecoder().decode(rejection.data));
}
var shouldHandle = (rejection && rejection.config && rejection.status !== 403 && rejection.config.headers && rejection.config.headers[HEADER_NAME] && !rejection.config.url.contains("logerror") && !rejection.config.url.contains("/ping") && !rejection.config.alreadyHandled);
if (shouldHandle) {
if (rejection.data) {
var message = "An error occurred:<br>" + rejection.data.status;
if (rejection.data.error) {
message += ": " + rejection.data.error
}
if (rejection.data.path) {
message += "<br><br>Path: " + rejection.data.path;
}
if (message !== "No message available") {
message += "<br><br>Message: " + _.escape(rejection.data.message);
} else {
message += "<br><br>Exception: " + rejection.data.exception;
}
} else {
message = "An unknown error occurred while communicating with NZBHydra:<br><br>" + JSON.stringify(rejection);
}
GeneralModalService.open(message);
} else if (rejection && rejection.config && rejection.config.headers && rejection.config.headers[HEADER_NAME] && rejection.config.url.contains("logerror")) {
console.log("Not handling connection error while sending exception to server");
}
return $q.reject(rejection);
}
};
});
nzbhydraapp.config(['$provide', '$httpProvider', function ($provide, $httpProvider) {
$httpProvider.interceptors.push('RequestsErrorHandler');
// --- Decorate $http to add a special header by default ---
function addHeaderToConfig(config) {
config = config || {};
config.headers = config.headers || {};
// Add the header unless user asked to handle errors himself
if (!specificallyHandleInProgress) {
config.headers[HEADER_NAME] = true;
}
return config;
}
// The rest here is mostly boilerplate needed to decorate $http safely
$provide.decorator('$http', ['$delegate', function ($delegate) {
function decorateRegularCall(method) {
return function (url, config) {
return $delegate[method](url, addHeaderToConfig(config));
};
}
function decorateDataCall(method) {
return function (url, data, config) {
return $delegate[method](url, data, addHeaderToConfig(config));
};
}
function copyNotOverriddenAttributes(newHttp) {
for (var attr in $delegate) {
if (!newHttp.hasOwnProperty(attr)) {
if (typeof($delegate[attr]) === 'function') {
newHttp[attr] = function () {
return $delegate.apply($delegate, arguments);
};
} else {
newHttp[attr] = $delegate[attr];
}
}
}
}
var newHttp = function (config) {
return $delegate(addHeaderToConfig(config));
};
newHttp.get = decorateRegularCall('get');
newHttp.delete = decorateRegularCall('delete');
newHttp.head = decorateRegularCall('head');
newHttp.jsonp = decorateRegularCall('jsonp');
newHttp.post = decorateDataCall('post');
newHttp.put = decorateDataCall('put');
copyNotOverriddenAttributes(newHttp);
return newHttp;
}]);
}]);