angularjs – Restangular – 如何覆盖错误拦截器

前端之家收集整理的这篇文章主要介绍了angularjs – Restangular – 如何覆盖错误拦截器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用AngularJS v1.2.16和Restangular v1.4.0,并想知道是否可以覆盖ErrorInterceptor.如果有,怎么样?如果不是,我该如何解决

我已经配置了一个错误拦截器,如下所示:

  1. RestangularProvider.setErrorInterceptor(
  2. function ( response ) {
  3. if ( response.status == 401 ) {
  4. dialogs.error("Unauthorized - Error 401","You must be authenticated in order to access this content.")
  5. .result.then( function () {
  6. $location.path("/login");
  7. });
  8. }
  9. else {
  10. // Some other unknown Error.
  11. console.log( response );
  12. dialogs.error(response.statusText + " - Error " + response.status,"An unknown error has occurred.<br>Details: " + response.data);
  13. }
  14. // Stop the promise chain.
  15. return false;
  16. }
  17. );

然后,在另一个地方,我进行了一个错误处理的POST调用.

  1. function saveApple( apple ) {
  2. Restangular.all("apple/save").post( apple ).then(
  3. function ( response ) {
  4. console.log("Saved");
  5. },function ( response ) {
  6. // This is not being called.
  7. console.log("Error with status code",response.status);
  8. }
  9. );
  10. }

据我所知,我的“第二个”错误处理程序没有被调用,因为我在ErrorInterceptor上返回false.

但我怎么能解决这个问题呢?我的应用程序中有很多“REST操作”,只有少数几个,我想在出现问题时进行自定义行为.

到目前为止,我想到的唯一解决方法是使ErrorInterceptor返回true,对于其他所有REST操作,我复制粘贴相同的错误处理程序(更常见的一个).但这将是我要做的最后一件事.

现在,像这样流淌:

> ErrorInterceptor>结束.

如果可能的话,我希望它是这样的:(仅适用于特定方法 – 不是全部).

> ErrorInterceptor> Especific_ErrorHandler(如果存在)>结束.

它也可以是这样的:

> Especific_ErrorHandler(如果存在)> ErrorInterceptor>结束.

无论哪种方式都适合我.

参考文献:

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

提前致谢!

这是你的代码
  1. RestangularProvider.setErrorInterceptor(
  2. function ( response ) {
  3. if ( response.status == 401 ) {
  4. dialogs.error("Unauthorized - Error 401","An unknown error has occurred.<br>Details: " + response.data);
  5. }
  6. // Stop the promise chain.
  7. return false;
  8. }
  9. );

你为什么使用拦截器?
– 因为你想要全面的对话框来显示错误信息.

停止承诺链,这是不好的做法?

我不知道.很多人使用错误回调;并且一个用于错误回调的用例是进行一些清理.如果你杀了承诺链,你如何确保清理完成?停止promise链意味着你的错误回调,你的catch块和你的finally块都不会被调用.

在文档中,清理完成后,您可以看到deferred.reject被传递.
https://github.com/mgonto/restangular#seterrorinterceptor
也许你误解了文档中的例子.

可能的解决方案#1

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

可能的解决方案#2

不要处理错误拦截器中的未知错误.

  1. RestangularProvider.setErrorInterceptor(
  2. function ( response ) {
  3. if ( response.status == 401 ) {
  4. dialogs.error("Unauthorized - Error 401","You must be authenticated in order to access this content.")
  5. .result.then( function () {
  6. $location.path("/login");
  7. });
  8. // Stop the promise chain.
  9. // all unauthorized access are handled the same.
  10. return false;
  11. }
  12. // Some other unknown Error.
  13. console.log( response );
  14. dialogs.error(response.statusText + " - Error " + response.status,"An unknown error has occurred.<br>Details: " + response.data);
  15. }
  16. // DON'T stop promise chain since error is not handled
  17. return true;
  18. }
  19. );

可能的解决方案#3

停止承诺链时调用拒绝.

  1. RestangularProvider.setErrorInterceptor(
  2. function ( response,deferred,responseHandler ) {
  3. if ( response.status == 401 ) {
  4. dialogs.error("Unauthorized - Error 401","You must be authenticated in order to access this content.")
  5. .result.then( function () {
  6. // continue promise chain in this callback.
  7. deferred.reject("unauthorized");
  8. $location.path("/login");
  9. });
  10. // Stop the promise chain here
  11. // all unauthorized access are handled the same.
  12. return false;
  13. }
  14. // Some other unknown Error.
  15. console.log( response );
  16. dialogs.error(response.statusText + " - Error " + response.status,"An unknown error has occurred.<br>Details: " + response.data);
  17. }
  18. // DON'T stop promise chain since error is not handled
  19. return true;
  20. }
  21. );

猜你在找的Angularjs相关文章