Restangular - How to override error hooks

I am using AngularJS v1.2.16 with Restangular v1.4.0 and would like to know if it is possible to override ErrorInterceptor. If so, how? if not, how can I work with him?

I configured the Interceptor error as follows:

RestangularProvider.setErrorInterceptor( function ( response ) { if ( response.status == 401 ) { dialogs.error("Unauthorized - Error 401", "You must be authenticated in order to access this content.") .result.then( function () { $location.path("/login"); }); } else { // Some other unknown Error. console.log( response ); dialogs.error(response.statusText + " - Error " + response.status, "An unknown error has occurred.<br>Details: " + response.data); } // Stop the promise chain. return false; } ); 

then in another place I make a POST call with error handling.

 function saveApple( apple ) { Restangular.all("apple/save").post( apple ).then( function ( response ) { console.log("Saved"); }, function ( response ) { // This is not being called. console.log("Error with status code", response.status); } ); } 

I understand that my “second” error handler is not called because I am returning false to ErrorInterceptor .

But how can I do this? I have many “REST operations” in my application, and only a few of them, I want to adjust the behavior when something goes wrong.

So far I have been thinking about what ErrorInterceptor return true needs to be done, and for all other REST operations I will copy the same error handler (more general). But that would be the last thing I would do.

Right now, flow as follows:

  • ErrorInterceptor> End.

If possible, I want it to be like this: (Only for certain methods - not for all).

  • ErrorInterceptor> Especific_ErrorHandler (if exists)> End.

it can also be like this:

  • Especific_ErrorHandler (if exists)> ErrorInterceptor> End.

In any case, it works for me.

Literature:

https://github.com/mgonto/restangular#seterrorinterceptor https://github.com/mgonto/restangular#how-can-i-handle-errors

Thank Advance

+7
angularjs restangular
source share
2 answers

Here is your code:

 RestangularProvider.setErrorInterceptor( function ( response ) { if ( response.status == 401 ) { dialogs.error("Unauthorized - Error 401", "You must be authenticated in order to access this content.") .result.then( function () { $location.path("/login"); }); } else { // Some other unknown Error. console.log( response ); dialogs.error(response.statusText + " - Error " + response.status, "An unknown error has occurred.<br>Details: " + response.data); } // Stop the promise chain. return false; } ); 

Why are you using an interceptor? - because you need dialogs in all sections to display error messages.

Stopping the promise chain, is this a bad practice?

I dont know. Many people use error callbacks; and one use case for callbacks is to do some cleanup. If you kill the promise chain, how do you ensure the cleanup? Stopping the promise chain means your error callbacks, your catch blocks, and your finally blocks will not be called.

In the docs cleanup is complete, you can see that deferred.reject is passed along. https://github.com/mgonto/restangular#seterrorinterceptor Perhaps you misunderstood the example that was in the docs.

Possible Solution # 1

  // DON'T stop the promise chain. return true; 

Possible Solution # 2

Do not handle unknown errors in the error hook.

 RestangularProvider.setErrorInterceptor( function ( response ) { if ( response.status == 401 ) { dialogs.error("Unauthorized - Error 401", "You must be authenticated in order to access this content.") .result.then( function () { $location.path("/login"); }); // Stop the promise chain. // all unauthorized access are handled the same. return false; } // Some other unknown Error. console.log( response ); dialogs.error(response.statusText + " - Error " + response.status, "An unknown error has occurred.<br>Details: " + response.data); } // DON'T stop promise chain since error is not handled return true; } ); 

Possible Solution # 3

call reject when you stop the promise chain.

 RestangularProvider.setErrorInterceptor( function ( response, deferred, responseHandler ) { if ( response.status == 401 ) { dialogs.error("Unauthorized - Error 401", "You must be authenticated in order to access this content.") .result.then( function () { // continue promise chain in this callback. deferred.reject("unauthorized"); $location.path("/login"); }); // Stop the promise chain here // all unauthorized access are handled the same. return false; } // Some other unknown Error. console.log( response ); dialogs.error(response.statusText + " - Error " + response.status, "An unknown error has occurred.<br>Details: " + response.data); } // DON'T stop promise chain since error is not handled return true; } ); 
+15
source share

Yes, you can create a custom httpErrorHandler .

  • First you need to create .factory , name it httpErrorHandler

 .factory('httpErrorHandler', function ($rootScope) { return { 'error': function (rejection) { if (rejection.status === 422) { $rootScope.$broadcast('422_error', rejection.data); } else if (rejection.status === 403) { $rootScope.$broadcast('403_error', rejection.data); } else { $rootScope.$broadcast('unknown', rejection.data); } return $q.reject(rejection); } }; } 
  • Then register that factory for $httpProvider interceptors

 .config(function ($httpProvider) { $httpProvider.interceptors.push('httpErrorHandler'); }); 

Then just catch the $rootScope events $rootScope in the application and process them, or add some logic inside the interceptor.

I hope this works as expected.

+2
source share

All Articles