sam*_*sam 7 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;
}
);
Run Code Online (Sandbox Code Playgroud)
然后,在另一个地方,我进行了一个错误处理的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);
}
);
}
Run Code Online (Sandbox Code Playgroud)
我明白,我的"第二"的错误处理程序不会被调用,因为我回来false
的ErrorInterceptor
.
但是我怎么能解决这个问题呢?我的应用程序中有很多"REST操作",只有少数几个,我想在出现问题时进行自定义行为.
到目前为止,我想到的唯一解决方法是ErrorInterceptor
返回true
,对于其他所有REST操作,我都复制粘贴相同的错误处理程序(更常见的一个).但这将是我要做的最后一件事.
现在,像这样流淌:
如果可能的话,我希望它是这样的:(仅适用于特定方法 - 不是全部).
它也可以是这样的:
无论哪种方式都适合我.
参考文献:
https://github.com/mgonto/restangular#seterrorinterceptor https://github.com/mgonto/restangular#how-can-i-handle-errors
提前致谢!
dno*_*zay 15
这是你的代码:
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;
}
);
Run Code Online (Sandbox Code Playgroud)
你为什么使用拦截器? - 因为你想要全面的对话框来显示错误信息.
我不知道.很多人使用错误回调; 并且一个用于错误回调的用例是进行一些清理.如果你杀了承诺链,你如何确保清理完成?停止promise链意味着你的错误回调,你的catch
块和你的finally
块都不会被调用.
在文档中,清理完成后,您可以看到deferred.reject
传递完成.
https://github.com/mgonto/restangular#seterrorinterceptor
可能你误解了文档中的例子.
// DON'T stop the promise chain.
return true;
Run Code Online (Sandbox Code Playgroud)
不要处理错误拦截器中的未知错误.
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;
}
);
Run Code Online (Sandbox Code Playgroud)
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;
}
);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8091 次 |
最近记录: |