Domanda

I want to use an interceptor for status code error handling. For example, when I'm loading an template through $route and I don't have the permission to do so I'm sending an 403 status code back from the server together with some html data, which I want to display instead of the originally requested template. Is this possible inside my interceptor in the 'responseError' function?

'responseError': function(rejection) {
  // do something on error EXAMPLE
  if (canRecover(rejection.htmlData)) {
    return responseOrNewPromise //here I want to add my server error html data and return that to the client instead of the originally requested data 
  }
  return $q.reject(rejection);
}

I know, that I could do a location.path change but i want to save the extra routing.

È stato utile?

Soluzione

From ngRoute docs:

$routeChangeError

Broadcasted if any of the resolve promises are rejected.

Add an event listener to when a promise resolve is rejected

app.run(function($rootScope){
   $rootScope.$on('$routeChangeError',function(angularEvent,current,previous,rejection){

     // console.log(rejection)

   })
})

I also recommend these tutorials:

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top