矩形-如何覆盖错误拦截器

山姆

我将AngularJS v1.2.16Restangular 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);
        }
    );
}

我了解,由于我正在返回false,因此未调用“第二”错误处理程序ErrorInterceptor

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

到目前为止,我想到的唯一变通方法是进行ErrorInterceptorreturn 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", "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;
    }
);

为什么要使用拦截器?-因为您需要全面显示错误消息的对话框。

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

我不知道。许多人使用错误回调。错误回调的一个用例是进行一些清理。如果您杀死了诺言链,如何确保清理完成?停止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

reject停止诺言链时致电

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章