【发布时间】: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