我已经在这几个小时了,我无法弄清楚为什么当我的rails后端引发了正确的错误时,angular不会触发我的错误回调。我正在使用角度1.2.0rc1。
根据文件:
non-GET "class" actions: Resource.action([parameters], postData, [success], [error])
我在保存产品操作期间在角度控制器中使用它:
$scope.saveProduct = function(product){
if (product.id) {
Product.update({id: product.id},{product: product}, function(data){
console.log('handle success')
}, function(){
console.log('handle error') //THIS IS NEVER OUTPUT!
});
}
}
以下是资源定义:
angular.module('sellbriteApp.resources').factory('Product', function ($resource) {
return $resource('/api/products/:id', { id: "@id" },
{
'create': { method: 'POST' },
'index': { method: 'GET', isArray: true },
'show': { method: 'GET', isArray: false },
'update': { method: 'PUT' },
'destroy': { method: 'DELETE' }
}
);
});
这是我的rails控制器:
def update
respond_to do |format|
if @product.update(product_params)
format.html { redirect_to [:edit, @product], notice: 'Product was successfully updated.' }
format.json { render 'products/show.json.jbuilder', status: :accepted }
else
format.html { render action: 'edit' }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
Rails在尝试使用重复的sku保存产品时返回422状态,并且我想在前端显示错误消息。
我希望angular应该执行更新调用中提供的错误处理功能,但我无法做到这一点。相反,在我的控制台中,我看到:
TypeError: Cannot read property 'data' of undefined
无用的堆栈跟踪:
TypeError: Cannot read property 'data' of undefined
at $http.then.value.$resolved (http://localhost:9000/bower_components/angular-resource/angular-resource.js:477:32)
at wrappedCallback (http://localhost:9000/bower_components/angular/angular.js:9042:59)
at wrappedCallback (http://localhost:9000/bower_components/angular/angular.js:9042:59)
at http://localhost:9000/bower_components/angular/angular.js:9128:26
at Object.Scope.$eval (http://localhost:9000/bower_components/angular/angular.js:9953:28)
at Object.Scope.$digest (http://localhost:9000/bower_components/angular/angular.js:9809:23)
at Object.$delegate.__proto__.$digest (<anonymous>:844:31)
at Object.Scope.$apply (http://localhost:9000/bower_components/angular/angular.js:10039:24)
at Object.$delegate.__proto__.$apply (<anonymous>:855:30)
at done (http://localhost:9000/bower_components/angular/angular.js:6542:45)
我错过了什么?
更新
显然这个http拦截器是相关的。如果我将此代码注释掉,则会调用错误函数。我已经从其他地方复制了这个片段并对其进行了修改,以便在用户未登录时点击rails api将用户重定向到sign_up页面。它必须是干扰,但我不知道应该如何解决它
App.config(['$httpProvider', function ($httpProvider) {
$httpProvider.responseInterceptors.push('securityInterceptor');
}]);
App.factory('securityInterceptor', ['$injector', '$location', '$cookieStore', function ($injector,$location,$cookieStore) {
return function(promise) {
var $http = $injector.get('$http');
return promise.then(null, function(response){
if (response.status === 401) {
$cookieStore.remove('_angular_devise_merchant');
toastr.warning('You are logged out');
$location.path('/sign_in');
}
});
};
}]);
答案 0 :(得分:7)
您也需要拒绝拦截器中的承诺,否则会将其视为“处理”异常。
所以:
App.factory('securityInterceptor', ['$injector', '$location', '$cookieStore', '$q', function ($injector,$location,$cookieStore, $q) {
return function(promise) {
var $http = $injector.get('$http');
return promise.then(null, function(response){
if (response.status === 401) {
$cookieStore.remove('_angular_devise_merchant');
toastr.warning('You are logged out');
$location.path('/sign_in');
}
return $q.reject(response);
});
};
}]);