使用restangular

时间:2015-12-04 06:28:32

标签: angularjs http-status-codes restangular

我使用restangular(customPut)来调用服务来更新'password'。调用时服务抛出错误422.错误处理程序和拦截器都无法捕获响应。我在网上搜索可能性,但没有运气。请在下面找到我的代码:

app.controller 'PasswordChangeController', ($scope, Flash, Restangular) ->
$scope.changePassword = (user) ->
    Restangular.one('users', 'change-password').customPUT(user).then ((SUCCESS) ->
      $scope.doLogout()
),(response) -> console.log(response)

http拦截器如下:

app.factory 'authWrapper', ($rootScope, $q, $window, Flash) ->
 request: (config) ->
  config.headers ?= {}
  if $window.localStorage.currentUser?
  user = JSON.parse $window.localStorage.currentUser
  config.headers.Authorization = "Bearer #{user['token']}"
  return config

# Handle response errors like 401 and 403
 responseError: (rejection) ->
  console.log rejection
  switch rejection.status
   when 401
    message = rejection.data.message or 'Your session has expired. Please log in again.'
    if rejection.config.url.search(/sign_in/) isnt -1
      Flash.alert(message).andRefresh()
    else
      Flash.setMsg 'alert', message
      $rootScope.$emit 'auth:unauthorized', (rejection.config.url.search(/sign_out/) isnt -1)
   when 403
    Flash.alert('You are not authorized to perform this action.').andRedirectTo '/'
   when 422
    Flash.alert('Password entered don\'t match with your password')
  $q.reject rejection

app.config ($httpProvider) ->
 $httpProvider.interceptors.push 'authWrapper'

还有其他方法可以捕获错误422吗?

0 个答案:

没有答案