角度资源没有调用我的错误回调函数

我已经在这几个小时,我无法弄清楚为什么角度不会触发我的错误回调当我的rails后端引发一个正确的错误。 我正在使用角度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 

尝试保存具有重复sku的产品时,Rails返回422状态,并且我想在前端显示错误消息。

我希望angular应该执行更新调用中提供的error handling函数,但我无法做到这一点。 而是在我的控制台中,我看到:

TypeError: Cannot read property 'data' of undefined使用无用的堆栈跟踪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 (:844:31) at Object.Scope.$apply (http://localhost:9000/bower_components/angular/angular.js:10039:24) at Object.$delegate.__proto__.$apply (:855:30) at done (http://localhost:9000/bower_components/angular/angular.js:6542:45) 

我错过了什么?

更新:

显然这个http拦截器是相关的。 如果我将此代码注释掉,则会调用错误函数。 我已经从其他地方复制了这个片段并对其进行了修改,以便将用户重定向到sign_up页面,如果他们在未登录时遇到rails api。它必须是干扰,但我不知道应该如何解决它。

 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'); } }); }; }]); 

你需要在拦截器中拒绝承诺,否则它被视为你已经“处理”了exception。

所以:

 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); }); }; }]);