【问题标题】:Context in EmberJS RSVP onerror handlerEmberJS RSVP onerror 处理程序中的上下文
【发布时间】:2014-02-23 15:52:31
【问题描述】:

我正在我的 EmberJS 应用程序中实现错误日志记录,就像 here 所描述的那样,它工作得很好。唯一让我失望的是如何正确处理来自 Ember RSVP onerror 事件的错误调用。

Ember 运行循环中产生的错误可以很好地使用messagestack 属性进行格式化,但是从 RSVP 引发的错误会返回标准的 XHR 响应并且没有额外的上下文。是否可以访问有关发生此错误时正在执行的 Ajax 调用的任何信息?

我正在使用 Ember 1.3.1 和 Ember Data 1.0.0+b6。

【问题讨论】:

  • 您的 ajax 调用是什么样的以及如何解决成功/错误挂钩?
  • @AlexLynham 我不会直接进行 Ajax 调用,Ember Data 会处理所有这些。
  • 我的猜测是这与 RSVP 如何解析 ajax 承诺以及如何处理从那里返回的值有关。
  • 在这里与 Ember Data 团队进行对话:github.com/emberjs/data/issues/1727
  • 啊,我和你在一起。所以标准的 XHR Ajax 错误对象不足以让您调试吗?获得更有用的日志的基线(理想场景)是什么?某种堆栈跟踪详细说明了错误发生的确切时间?

标签: ember.js ember-data rsvp.js


【解决方案1】:

我正在使用一种肮脏的解决方法从 RSVP 内部获取上下文。您可以覆盖 RVSP.Promise._onerror 方法并包含一些数据。 'this' 对象具有 _label 属性,其中包含有时有用的模型信息。 我的解决方案仍然不理想,但它是一些东西。

#RSVP _onerror hack
#put this before creating application
oldMethod = Em.RSVP.Promise.prototype._onerror
Em.RSVP.Promise.prototype._onerror = (reason) ->
  reason.label = this._label
  oldMethod(reason)

App = Ember.Application.create(options)

几乎没有改进的代码来挂钩标准的 onerror 方法

 Ember.RSVP.configure('onerror', (error) ->
   #handle situation when user in other tab logout from application.
   if error.status == 401 #not authorized
     window.location = '/login'
   else
     niceError = unless error.stack
       message = {
         label: error.label,
         status: error.status,
         statusText: error.statusText,
         state: error.state(),
         readyState: error.readyState,
         responseText: error.responseText.replace(/<(?:.|\n)*?>/gm, '').substr(0,100)
       }
       new Error(Ember.inspect(message))
     else
       error

 #here is method to send notification about error
 MessageApp.errorHandler('RSVP', niceError)

【讨论】:

    猜你喜欢
    • 2013-06-16
    • 1970-01-01
    • 2010-11-03
    • 2016-01-16
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    • 2011-01-03
    • 2021-08-28
    相关资源
    最近更新 更多