Really can not find good documentation about http interceptors in Angular js. While handling errors coused by ng-include
i can intercept responseError
by using this:
app.config(function ($httpProvider) {
$httpProvider.interceptors.push('templateInterceptor');
});
// register the interceptor as a service
app.factory('templateInterceptor', function($q) {
return {
'responseError': function(rejection) {
var isTemplate = !!rejection.config.url.match(/^content/g);
if (isTemplate) {
// here we add error message, but how this message appesrs in the place of ng-include
rejection.data = '<div><template-error url="\''+ (rejection.config.url) + '\'"><strong>Error from interceptor.</strong></template-error></div>';
return rejection;
} else {
return $q.reject(rejection);
}
}
}
});
This code was taken from this question how to catch angular ng-include error .
I don't get, how interceptors works? What they must return? How to use rejection
parameter that passed to the responseError
interceptor? In rejection
the data
property is used to include error message into the place of failed ng-include
directive, how this works?