I have implemented a default handler for handling http errors in my angularjs app as shown below:
myapp.config([ '$httpProvider', function($httpProvider) {
$httpProvider.responseInterceptors.push('errorInterceptor')
}])
The errorInterceptor
service is responsible for displaying error details in an alert field located at the top of the current page.
However, when I need to handle a specific error differently (for example, when the query is executed within a modal and I only want to display the alert in that modal), I do this:
$http.get('/my/request').then(success, specificErrorHandling)
Even though the specificErrorHandling
function is triggered, the errorInterceptor
is still activated, resulting in the error being reported twice. Is there a way to prevent this from happening?
More importantly, is there an Angular method to handle errors that have not been dealt with along the promise chain, similar to how exceptions are handled by the top-level error handler in a server application without having to catch them?
Edit: Here is the code for the interceptor as requested by Beetroot-Beetroot in the comments:
@app.factory 'errorInterceptor', [ '$q', 'alertsHandler',
($q, alertsHandler) ->
success = (response) ->
response
failure = (response) ->
alertsHandler.raise(response)
(promise) ->
promise.then success, failure
]