我将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);
}
);
}
我了解,由于我正在返回false
,因此未调用“第二”错误处理程序ErrorInterceptor
。
但是我该如何解决呢?我的应用程序中有很多“ REST操作”,只有少数几个,当出现问题时,我想要自定义行为。
到目前为止,我想到的唯一变通方法是进行ErrorInterceptor
return true
,对于其他所有REST操作,我都复制粘贴相同的错误处理程序(更通用的错误处理程序)。但这将是我要做的最后一件事。
现在,像这样流动:
如果可能的话,我希望它像这样:(仅适用于特定方法-并非全部)。
也可以像这样:
无论哪种方式对我都有效。
参考文献:
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也许您误解了文档中的示例。
// DON'T stop the promise chain.
return true;
不要在错误拦截器中处理未知错误。
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;
}
);
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] 删除。
我来说两句