本文介绍了矩形-如何覆盖错误拦截器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Restangular v1.4.0 中使用了 AngularJS v1.2.16 ,并且想知道是否有可能重写ErrorInterceptor.如果是,怎么办?如果没有,我该如何解决?

I am using AngularJS v1.2.16 with Restangular v1.4.0, and would like to know if it's possible to override the ErrorInterceptor. If yes, how? if no, how can I work it around?

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

I've configured an error Interceptor like this:

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呼叫.

then, on another place, I make a POST call with an 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);
        }
    );
}

我知道,没有调用我的第二"错误处理程序,因为我在 ErrorInterceptor 上返回了 false .

I understand that, my "second" error handler is not being called because I'm returning false on the ErrorInterceptor.

但是我该如何解决呢?我的应用程序中有很多"REST操作",只有少数几个,当出现问题时,我想要自定义行为.

But how can I work this around? I have lots of "REST operations" in my app, and only few of them, I want a customized behavior when something goes wrong.

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

So far, the only work-around I thought of, is to make the ErrorInterceptor return true, and for every other REST operations, I copy-paste the same error handler (a more general one). But this would be the last thing I would do.

现在,像这样流动:

  • ErrorInterceptor>结束.

如果可能的话,我希望它像这样:(仅适用于特定方法-并非全部).

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

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

它也可以像这样:

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

这两种方法都对我有用.

Either way works for me.

参考文献:

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", "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 want dialogs across the board for displaying error messags.

我不知道.许多人使用错误回调.错误回调的一个用例是进行一些清理.如果您杀死了诺言链,如何确保清理完成?停止promise链意味着您的错误回调,您的 catch 块和您的 finally 块将不会被调用.

I don't know. Many people use error callbacks; and one use-case for error callbacks is to do some cleanup. If you kill the promise chain, how do you ensure the cleanup is done? Stopping the promise chain means your error callbacks, your catch blocks, and your finally blocks won't get called.

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

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

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

可能的解决方案#2

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

Possible solution #2

Don't handle unknown errors in the error interceptor.

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

这篇关于矩形-如何覆盖错误拦截器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 03:19
查看更多