2014-01-31 2 views
1

here에 설명 된대로 EmberJS 응용 프로그램에 오류 로깅을 구현하고 있습니다. 나를 쫓아 버리는 유일한 부분은 Ember RSVP onerror 이벤트의 오류 호출을 올바르게 처리하는 방법입니다.EmberJS RSVP onerror handler의 컨텍스트

Ember 실행 루프에서 생성 된 오류는 messagestack 속성으로 멋지게 형식화되지만 RSVP에서 발생한 오류는 표준 XHR 응답을 반환하고 추가 컨텍스트는 제공하지 않습니다. 이 오류가 발생했을 때 Ajax 호출이 실행되고 있었는지에 대한 정보에 액세스 할 수 있습니까?

Ember 1.3.1 및 Ember Data 1.0.0 + b6을 사용하고 있습니다.

+0

당신의 아약스 호출처럼 중요시하는 점은 무엇과 어떻게 당신에게 성공/오류 후크를 해결의 OnError의 standart에 걸리지? –

+0

@AlexLynham 직접 Ajax 호출을하지는 않지만 Ember Data가 모든 것을 처리합니다. –

+0

내 생각 엔 RSVP가 ajax 약속을 어떻게 해결하는지와 그로부터 리턴 된 값을 어떻게 처리 할 수 ​​있을지와 관련이있다. –

답변

0

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) 

그리고 약간 개선 된 코드는 방법

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)