加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 08:34:07 所属栏目:安全 来源:网络整理
导读:我正在使用AngularJS v1.2.16和Restangular v1.4.0,并想知道是否可以覆盖ErrorInterceptor.如果有,怎么样?如果不是,我该如何解决? 我已经配置了一个错误拦截器,如下所示: RestangularProvider.setErrorInterceptor( function ( response ) { if ( respons
我正在使用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;
    }
);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读