In my current situation, I'm using an $http interceptor to catch response errors and display a simple message to let the user know something went wrong. But now I want to make a difference between errors that came from requests that are sent during the loading of a page or requests that are sent due to an action of the user itself (fe pressing a button, entering a field ...).
Example of the $httpInterceptor I want to have:
return {
responseError: function (response) {
// IF THE REQUEST IS FROM A PAGE LOAD
// DO SOMETHING
// IF THE REQUEST IS NOT FROM A PAGE LOAD
// DO SOMETHING ELSE
return $q.reject(response);
}
};
To make it a bit more clear:
CASE 1: A user is waiting for a page to load:
All the requests above are part of the page load and if one of them returns an error, it should have a specific behavior.
Requests that are triggered not because of a page load:
CASE 2: User presses a button:
These requests are not part of the page load and in case of an error should act differently.
Is there any way I can somehow make a difference between these 2 cases? As my project is quite large, I don't want to change the code of every $http request to identify it is from a page load or not, so that is not a solution for me.
You can create some kind or regular expression, and testing if came from desire url and make choices.
.service('RequestsInterceptor', [
'$q',
function(
$q,
) {
var UNAUTHORIZED_CODE = 401;
var FORBIDDEN_CODE = 403;
var NOTFOUND_CODE = 404;
var INTERNAL_ERROR = 500;
this.request = function(config) {
//logic to send
return config || $q.when(config);
};
this.responseError = function(rejection) {
console.log(rejection.config.url)
var apiPattern = /\/api\//;
if(apiPattern.test(rejection.config.url)) {
//do your logic when url are api
}else{
//do your logic when url are templates
}
return $q.reject(rejection);
};
}
]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.