【问题标题】:Restangular Error isn't caught on first try第一次尝试时未发现 Restangular 错误
【发布时间】:2018-02-28 22:55:49
【问题描述】:

这两天我一直在努力解决这个问题,但我被困住了......

当我使用应该出错的数据调用我的 api(在 @bulkValidate 函数中)时,Restangular 正在跳过我的错误处理程序,只是默默地失败。然后,如果我尝试再次或在它按预期工作并且正确显示错误 toast 之后的任何时间传递相同的数据。我可以在网络中看到在任何一种情况下都从 api 发送完全相同的 400 消息,所以我知道它工作正常。

在翻阅 SO 帖子后,我发现 $qProvider.errorOnUnhandledRejections(false) 让我的错误传递到我的自定义错误处理程序,所以当我给这个函数一个 true 值时,我现在收到这条消息的错误:

Error: [$sce:itype] Attempted to trust a non-string value in a content requiring a string: Context: html

我认为这是由<br/> 引起的,我正在输入 toastr 消息,但即使我将其删除,第一个错误仍然没有被我的catch 捕获。我还尝试按照this post 中的指示将错误消息包装在 $sce.trustAsHtml 中,但我仍然收到错误消息。

@bulkValidate = ->
  # Checked items live in approval_list
  if approval_list.length > 0
    orchestration.bulk_validate approval_list, host
    .then (res)->
      helper.displayToast 'success', 'Success', 'The selected categories have been scheduled for publishing.'
      $scope.$broadcast 'refresh_queue'
    .catch (err)->
      console.log(err)
      if err.status == 400
        msg = ''
        for onemsg in err.data.errors
          if msg.length > 0 then msg = msg + '<br/><br/>'
          msg = msg + onemsg
        if err.data.num_validated != undefined
          num_validated = err.data.num_validated
          errorMsg = num_validated + ' categories have been scheduled for publishing<br/><br/>. Some categories could not be published due to validation errors: <br/><br/>' + msg
          helper.displayToast 'error','Error',  errorMsg, {timeOut: 120000, showDuration: 12000}
        else
          helper.displayToast 'error','Error', 'No categories were published due to validation errors: ' + msg, {timeOut: 120000, showDuration: 12000}

任何帮助或建议将不胜感激!

【问题讨论】:

    标签: javascript angularjs coffeescript restangular toastr


    【解决方案1】:

    好吧,我终于弄明白了。似乎我的错误被另一个处理程序中的 Restangular.setErrorInterceptor 捕获,在 user.auth 组件的链中更进一步。

    Restangular.setErrorInterceptor (response, deferred, responseHandler)->
      if response.status == 403
        helper.displayToast 'error', null, response.data.detail
        if isClient() then $state.go 'sites_list'
        else $state.go 'clients'
      if response.status == 400
        if response.config.data?.clone_category_id
          modalService.open 'error-list', ()->
            md = this
            md.button =
              text: [
                'Okay'
              ],
              active: 0
            if typeIsArray(response.data)
              msgs = ''
              for item in response.data
                if msgs.length > 0
                  msgs += ", "
                msgs += item
              md.message = msgs
            else
              md.message = response.data
    
            @close = ->
              modalService.close()
            return
        else
          helper.displayToast 'error', null, response.data
      if response.status == 500 or response.status == -1
        helper.displayToast 'error', null, "There was an error processing your request"
      return true
    
    return
    

    在这种情况下,我的 400 错误没有response.config.data.clone_category_id,因此它被传递给else,从而它被发送到带有 response.data 的 toastr 助手(在这种情况下,它是一个对象而不是一连串的错误,就像它所期望的那样)。这就是第一个错误得到 [$sce:itype] 错误的原因。我在条件中添加了这个else if,它允许错误传递给它们各自的处理程序并修复了问题。

    else if response.data?.errors
      # pass errors to their respective handlers
      return
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-13
      • 1970-01-01
      相关资源
      最近更新 更多