Monday 15 February 2010

javascript - Angular: Default handler for unhandled http errors -


In my angular applications, I have defined a default handler for HTTP errors like this:

 Myapp.config (['$ httpProvider', function ($ httpProvider) {$ httpProvider.responseInceptceptors.push ('Error Interceptor')}])   

Where Error InterApter is a service that displays some details about the error in an alert field at the top of the current page.

Now, when I have to handle a specific error in some other way (say that a query is triggered in the query, and I only want to display a warning in this model, and not at the page level) :

  $ http.get ('/ my / request'). Again (success, typical aerhardling)   

angular does special aerharding but still triggers my errorinterceptor , so my error is twice Is there any way to avoid it?

More generally, there is a similar way of handling errors that do not already pay attention to the promise chain, just like the top of a server app Do not need to control the exception handling of the level error handler?

Edit: As requested in Beatrot-Bettot comments, this is the code for me Interceptor:

  @ app. Factory 'errorInterceptor', ['$ q', 'AlertHandler', ($ q, AlertHandler) - & gt; Success = (reaction) - & gt; Reaction failure = (reaction) - & gt; Alerthandler Arise (reaction) (promise) - & gt; I promise. Success, failure]    

We have something like this

if If we handle the HTI error, then we have errorHandled: true

  $ http ({method: 'GET', Url: '/ my / url', errorHandled: True}). Then (function () {...}, function () {...});   

And then in the block for the response: error: function (rejection) {...} We can see that by looking at the flag disapproval Is configured .config.errorHandled and if not - then we pop a toast dialog with error. Code looks like something

  function (disapproval) {if configured (reject !, panic and shuffle and rejection data.) {Toastr.error (rejection.data.message, 'error '); } Return $ q.reject (rejection); }   

The possibility of writing to someone "errorHandled: true" There are thin slabs without adding a handler, the likelihood of having 2 error indicators is also slim because we use it - but actually 2 indicators No one is better ..

It would be great if we have a promise to ask whether this error handler or then is not down the chain, but we do not have it anywhere So to receive.

No comments:

Post a Comment