在拦截器错误调用 successCallback 后使用 $promise.then()
Using $promise.then() after Interceptor erroneously calls successCallback
我想在拦截器完成基本消息处理后使用 $promise.then(successCallback,errorCallback)。如果我收到错误 400,401 等,它总是 运行 进入 successCallback 并且永远不会进入 errorCallback 等。如果我删除拦截器它工作正常。我还发现我可以在拦截器中创建一个新的承诺来解决这个问题,但我认为这不是最好的方法。在我的例子中,我只想要一个用于一些基本消息处理的拦截器,然后在主控制器中继续使用主要逻辑。目前这是不可能的,因为如果出现错误,我总是 运行 进入 successCallback。
我理解错了吗?
这是我想要的示例:
var resourceObj = $resource("url/abc/:action/:id", {}, {
getMember: {
method: 'GET',
params: { action: 'member' },
interceptor: {
responseError: function(response) {
console.log("Interceptor responseError");
//Show some default error messages here
return response; //create new promise with $http(response); to solve the problem?
},
response: function(response) {
console.log("Interceptor responseSuccess");
//Show some default success messages here
return response; //create new promise with $http(response); to solve the problem?
}
}
}
});
myModule.controller('myCtrl', function($scope) {
$scope.checkMember() = function(memberId) {
resourceObj.getMember({ id: memberId }, {}).$promise.then(
function(responseOK) { //successCallback
console.log(responseOK);
$scope.testMember = responseOK.data; // or if no interceptor is used responseOK.toJSON()
//do some stuff here after async call is finished
},
function(responseError) { //errorCallback, never called in error case if an interceptor is used.
console.log(responseError);
//do maybe some advanced error handling here after async call is finished
}
);
}
});
重新抛出错误响应很重要
var resourceObj = $resource("url/abc/:action/:id", {}, {
getMember: {
method: 'GET',
params: { action: 'member' },
interceptor: {
responseError: function(response) {
console.log("Interceptor responseError");
//Show some default error messages here
̶r̶e̶t̶u̶r̶n̶ ̶r̶e̶s̶p̶o̶n̶s̶e̶;̶
//IMPORTANT re-throw error response
throw response;
},
response: function(response) {
console.log("Interceptor responseSuccess");
//Show some default success messages here
return response;
}
}
}
});
如果简单地返回错误响应,它将错误地转换从拒绝到成功。
我想在拦截器完成基本消息处理后使用 $promise.then(successCallback,errorCallback)。如果我收到错误 400,401 等,它总是 运行 进入 successCallback 并且永远不会进入 errorCallback 等。如果我删除拦截器它工作正常。我还发现我可以在拦截器中创建一个新的承诺来解决这个问题,但我认为这不是最好的方法。在我的例子中,我只想要一个用于一些基本消息处理的拦截器,然后在主控制器中继续使用主要逻辑。目前这是不可能的,因为如果出现错误,我总是 运行 进入 successCallback。 我理解错了吗? 这是我想要的示例:
var resourceObj = $resource("url/abc/:action/:id", {}, {
getMember: {
method: 'GET',
params: { action: 'member' },
interceptor: {
responseError: function(response) {
console.log("Interceptor responseError");
//Show some default error messages here
return response; //create new promise with $http(response); to solve the problem?
},
response: function(response) {
console.log("Interceptor responseSuccess");
//Show some default success messages here
return response; //create new promise with $http(response); to solve the problem?
}
}
}
});
myModule.controller('myCtrl', function($scope) {
$scope.checkMember() = function(memberId) {
resourceObj.getMember({ id: memberId }, {}).$promise.then(
function(responseOK) { //successCallback
console.log(responseOK);
$scope.testMember = responseOK.data; // or if no interceptor is used responseOK.toJSON()
//do some stuff here after async call is finished
},
function(responseError) { //errorCallback, never called in error case if an interceptor is used.
console.log(responseError);
//do maybe some advanced error handling here after async call is finished
}
);
}
});
重新抛出错误响应很重要
var resourceObj = $resource("url/abc/:action/:id", {}, {
getMember: {
method: 'GET',
params: { action: 'member' },
interceptor: {
responseError: function(response) {
console.log("Interceptor responseError");
//Show some default error messages here
̶r̶e̶t̶u̶r̶n̶ ̶r̶e̶s̶p̶o̶n̶s̶e̶;̶
//IMPORTANT re-throw error response
throw response;
},
response: function(response) {
console.log("Interceptor responseSuccess");
//Show some default success messages here
return response;
}
}
}
});
如果简单地返回错误响应,它将错误地转换从拒绝到成功。