【发布时间】:2015-07-10 04:03:42
【问题描述】:
我正在使用 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(如果存在)> End。
也可以是这样的:
- Especific_ErrorHandler(如果存在)> ErrorInterceptor > End。
任何一种方式都适合我。
参考资料:
https://github.com/mgonto/restangular#seterrorinterceptor https://github.com/mgonto/restangular#how-can-i-handle-errors
提前致谢!
【问题讨论】:
标签: angularjs restangular