Context in EmberJS RSVP onerror handler

470 Views Asked by At

I am implementing error logging in my EmberJS application much as is described here and it's working pretty well. The only part that is throwing me off is how to properly handle error calls from the Ember RSVP onerror event.

Errors produced from within the Ember run loop are nicely formatted with message and stack properties, but errors raised from RSVP give back a standard XHR response and no additional context. Is it possible to access any information about what Ajax call was being executed when this error occurred?

I am using Ember 1.3.1 and Ember Data 1.0.0+b6.

1

There are 1 best solutions below

0
On

I'm using a dirty workaround to get context from RSVP internals. You can overwrite RVSP.Promise._onerror method and include some data. 'this' object has _label property which contains sometime useful info about model. My solution is still not ideal but it is something.

#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)

And little improved code to hook on standart onerror method

 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)