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