0

I've been trying to wrap my head around this now for 2 days and I'm stuck...

When I call my api (in the @bulkValidate function) with data that is supposed to error, Restangular is skipping my error handler and just silently failing. Then, if I try to pass the same data again or anytime after it works as expected and the error toast is properly displayed. I can see in the network that the exact same 400 message is being sent from the api in either case so I know that is working properly.

After digging through SO posts, I found that $qProvider.errorOnUnhandledRejections(false) is letting my errors pass through to my custom error handlers, so when I gave this function a true value, I now get errors with this message:

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

which I thought was being caused by the <br/> I'm putting in the toastr message but it happens even when I remove it and the first error is still not getting caught by my catch regardless. I've also tried wrapping the error message in $sce.trustAsHtml as directed in this post but I still get the error.

@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}

Any help or suggestions would be greatly appreciated!

catch22
  • 391
  • 1
  • 3
  • 13

1 Answers1

0

Okay I finally got this figured out. It seems my error was being caught by Restangular.setErrorInterceptor in another handler further up the chain in the user.auth component.

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

My 400 error in this case didn't have response.config.data.clone_category_id so it was getting passed to the else, whereby it was sent to the toastr helper with response.data (which in this case was an object instead of an string of errors like it was expecting). This is why the first error was getting the [$sce:itype] error. I added this else if to the conditions and it allows the errors pass through to their respective handlers and fixed the problem.

else if response.data?.errors
  # pass errors to their respective handlers
  return
catch22
  • 391
  • 1
  • 3
  • 13