I have service that integrates with external API. This API is inconsistent and it is unlikely to change.
Inside API there are 2 methods of reporting error, one is response with HTTP 500 code, other is normal 200 HTTP reponse, but with status
in JSON set to "error".
Handling 500 error is working fine, I have just created interceptor (ngResource interceptor, not $http one) with responseError
method that will display error message to user when there is any 500 response from API:
.factory('ApiErrorHandling', ['ngDialog', function(ngDialog) {
return {
responseError: function(response) {
console.log(response);
var error_dialog = ngDialog.open({
template: '/api/error.html',
className: 'error',
preCloseCallback: function() {
if (response.data.status == 'fatal') {
window.location.href = '/';
} else if (response.data.status == 'error') {
var dialogs = ngDialog.getOpenDialogs();
for (var i in dialogs) {
var dialog = dialogs[i];
if (dialog != error_dialog.id) {
ngDialog.close(dialog);
}
}
}
},
data: {
code: response.data.code,
status: response.data.status,
message: response.data.message,
httpCode: response.status,
}
});
},
}
}])
.factory('ApiMethod', ['$resource', 'ApiErrorHandling', function($resource, ApiErrorHandling) {
return $resource('/api/method/', {}, {
query: {method:'POST', interceptor: ApiErrorHandling},
});
}])
But I have problem with handling error responses with status
. I don't want normal success callback (passed into method from resource instance) to be called when error occurs, I want one global error handler, but nothing I've tried so far worked. Each time response is passed into callback. I've tried so far:
- returning $q.reject in
response
method from interceptor - changing status code in response to 500 inside
response
method from interceptor - changing or deleting response.resource in
response
method from interceptor
Ideal solution will be when normal callback won't be called, but my responseError
method from interceptor will be called instead.