微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在使用AngularJS v1.2.16和Restangular v1.4.0,并想知道是否可以覆盖ErrorInterceptor.如果有,怎么样?如果不是,我该如何解决

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

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;
    }
);

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

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);
        }
    );
}

据我所知,我的“第二个”错误处理程序没有被调用,因为我在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

提前致谢!

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

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

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

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

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

可能的解决方案#1

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

可能的解决方案#2

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

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;
    }
);

可能的解决方案#3

停止承诺链时调用拒绝.

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;
    }
);

原文地址:https://www.jb51.cc/angularjs/141390.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐