-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
47 lines (45 loc) · 1.31 KB
/
index.js
File metadata and controls
47 lines (45 loc) · 1.31 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
function requestAjaxError(req, res, next, logoutCallback) {
return function(moduleError, apiResponse, statusCode) {
var err;
if (res.locals._requestAjaxErrorHandled) {
return;
}
res.locals._requestAjaxErrorHandled = true;
if (logoutCallback && statusCode === 401) {
logoutCallback(req, res, next);
return;
}
if (moduleError) {
err = moduleError;
}
else {
err = new Error(
'API request returned with status: ' + statusCode +
"\n" +
(apiResponse ? apiResponse.body : '')
);
if (apiResponse && apiResponse.request && apiResponse.request.uri && apiResponse.request.uri.href) {
err.url = apiResponse.request.uri.href;
}
}
if (statusCode === 403) { // for security resons
err.status = 404;
}
else {
err.status = statusCode || 500;
}
return next(err);
};
}
module.exports = requestAjaxError;
module.exports.backbone = function(req, res, next, logoutCallback){
return function(model, error, options) {
var errorHanlder = requestAjaxError(req, res, next, logoutCallback);
if (options && options.xhr && options.xhr.response) {
return errorHanlder(error, options.xhr.response, options.xhr.response.statusCode);
}
else {
return errorHanlder(error);
}
};
};