任何想法處理401錯誤?灰燼 - 數據處理401的
在應用程序初始化程序中,我推遲準備並通過ember-data獲取當前用戶。如果我收到一個401應用程序死亡,並變得無法使用。我想處理這個錯誤,然後提前回復。我似乎無法找到解決方法。任何信息,將不勝感激!
要點在這裏:https://gist.github.com/unknpwn/6126462
我注意到有一個similar topic here,但它似乎是過時的。
任何想法處理401錯誤?灰燼 - 數據處理401的
在應用程序初始化程序中,我推遲準備並通過ember-data獲取當前用戶。如果我收到一個401應用程序死亡,並變得無法使用。我想處理這個錯誤,然後提前回復。我似乎無法找到解決方法。任何信息,將不勝感激!
要點在這裏:https://gist.github.com/unknpwn/6126462
我注意到有一個similar topic here,但它似乎是過時的。
Application.initializer
不是放置此邏輯的正確位置。它是同步的,它的目的是做一些事情,比如將自定義對象添加到IOC容器等等。這個代碼更適合model
掛鉤ApplicationRoute
。
App.ApplicationRoute = Ember.Route.extend({
beforeModel: function() {
return App.User.find({ filter: "authenticated" });
},
setupController: function(controller, model) {
controller.set('loggedIn', true);
controller.set('currentUser', model);// or some property of that model
},
events: function() {
error: function(err) {
// error handler called in case of an error.
// show the error message to user here
// or transition to another route
}
}
});
上一個答案已過時。在當前Ember版本(1+)events
已被棄用,您應該使用actions
對象(而不是函數)。
灰燼例如:
App.ApplicationRoute = Ember.Route.extend({
actions: {
error: function(err) {
// error handler called in case of an error.
// show the error message to user here
// or transition to another route
}
}
});
灰燼CLI例如:
import Ember from 'ember';
export default Ember.Route.extend({
actions: {
error: function(err) {
// error handler called in case of an error.
// show the error message to user here
// or transition to another route
}
}
});
有了這些動作處理程序錯誤將很好地泡在主應用程序的路線,如果你不前,你的路線停止。
太棒了!模型存在之前我不知道。儘管我嘗試從我的API獲取經過身份驗證的用戶,然後向用戶顯示登錄表單以代替內容,但仍然遇到了捕獲預期的401錯誤的問題。當401回來時,錯誤功能似乎沒有被擊中。那裏有任何想法? – bmherold
有一些錯誤承諾會吞噬一些異常,這可能是一個。嘗試在服務器上引發500錯誤,看看是否觸發處理程序。 –
從Ember Data v1.0.0-b3開始,這是可行的(附加說明'events'應該重命名爲'actions',因爲前者已被棄用。 –