簡體   English   中英

EmberJS RSVP onerror處理程序中的上下文

[英]Context in EmberJS RSVP onerror handler

我正在EmberJS應用程序中實現錯誤日志記錄,正如此處所述,它運行良好。 唯一讓我失望的是如何正確處理來自Ember RSVP onerror事件的錯誤調用。

在Ember運行循環中產生的錯誤可以使用messagestack屬性很好地格式化,但是從RSVP引發的錯誤可以返回標准的XHR響應,並且沒有其他上下文。 發生此錯誤時,是否可以訪問有關正在執行什么Ajax調用的任何信息?

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

我正在使用一種骯臟的解決方法來從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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM