I am currently working on creating a custom HTTP interceptor in Angular and I am looking to generate an error from the response
of the $httpProvider
interceptor.
According to the provided documentation:
response
: Interceptors are triggered by the http response object. The function has the ability to modify the response object or create a new one. It must return the response object directly, or as a promise containing the response or a new response object.
responseError
: The interceptor is called when a previous interceptor throws an error or resolves with a rejection.
I am specifically interested in the highlighted part in the quote above, where a response with status 200 (based on a specific condition) is sent to responseError
, allowing me to manage the error.
If I do not return a response, I encounter the following error:
Cannot read property 'data' of undefined
While I do not wish to return the response, I do want to pass it to the next handler, which is responseError
.
Is there any way to achieve this? I hope I have explained the issue clearly. Thank you.
Update (Code Below):
app.config(['$httpProvider', function($httpProvider) {
interceptor.$inject = ['$q', '$rootScope'];
$httpProvider.interceptors.push(interceptor);
function interceptor($q, $rootScope) {
return {
response: response,
responseError: responseError
};
function response(response) {
if (response.data.rs == "F") {
// I intend to direct it to responseError -->
} else {
return response;
}
}
function responseError(response) {
// My intention is to handle the error here
}
}
}]);