My setup includes a basic Angular http
interceptor designed to manage errors. I need to verify if the received data
is categorized as a string
, treating it as an error rather than a success.
'response': function(response) {
if(typeof response.data === 'string') {
response.status = 422;
return $q.reject(response);
} else {
return response;
}
},
'responseError': function(rejection) {
return $q.reject(rejection);
}
Currently, when the data
is identified as a string
, it enters the appropriate if statement, changes the status, and returns. However, Angular doesn't interpret this as an error. In my http
function, it doesn't trigger either the success or error callbacks. When the API sends back a genuine error, it correctly goes into 'responseError' and executes the error callback as expected.
$http({
method: 'POST',
url: "http://mydomain/api/login",
data: $.param({
username: "test",
password: "test"
}),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).then(successCallback(response), errorCallback(response));
How can I modify my interceptor to return an error that Angular can identify in order to activate the error callback from my http
function?