Explore Different Approaches
1. Dealing with Response Errors:
To handle response errors effectively, consider creating a factory that generates a response handler. In this specific scenario where only failed responses matter, prioritize implementing and returning an object featuring the responseError method:
function customFactory($q) {
return {
responseError: function(response){
if (response.status === 404) {
// process the error
return $q.resolve(response)
}
return $q.reject(response);
}
}
}
If managing the response is not feasible, reject it to allow other handlers down the chain to take action.
2. Aborting Requests and Managing Errors:
In case of aborting requests within the `request()` method, address the aborted request in the `responseError()` method. You can incorporate distinctive properties or code to differentiate it from standard response errors:
function customFactory($q) {
return {
request: function(config) {
if (/bad/.test(config.url)) {
config.statusText = 'Non existing path';
config.status = 404;
return $q.reject(config);
}
return config;
},
responseError: function(response) {
if (response.status === 404) {
// Handle 404 errors
response.status = 200;
response.statusText = 'OK';
response.data = {fake: 'data'}
return $q.resolve(response);
}
return $q.reject(response);
}
}
}
Check out this live example for a demonstration on how requests are checked and potentially aborted before being handled in the `responseError()` method.
3. Leveraging Cache Functionality:
Another straightforward approach involves redirecting a request to a cached route as a solution:
function customFactory($cacheFactory, $q) {
var cache = $cacheFactory('fakePages');
cache.put('cachedUrl', {fake: 'data'});
return {
request: function(config) {
if (/bad/.test(config.url)) {
config.url = 'cachedUrl';
config.cache = cache;
}
return config;
}
}
}
For an illustration of the implementation using cache functionality, refer to this sample plunk.