简体   繁体   English

在RESTAdapter中使用ember-data时,如何处理emberJs中的HTTP错误?

[英]how to handle HTTP errors in emberJs while using ember-data with RESTAdapters?

I have a route /products . 我有一条路线/products In App.ProductsRoute , I am using a setupController hook to assign list of products fetched from server to local App.Product objects. App.ProductsRoute ,我使用setupController挂钩将从服务器获取的产品列表分配给本地App.Product对象。

I am setting the model in setupController hook as : 我在setupController挂钩中将模型设置为:

self_controller.set('model', self_controller.store.find('product'));

This works well when HTTP status is 200. But when server returns some HTTP error (like 500-Internal Server Error, 401-Unauthorized Errors, etc) I am getting error as JSON.parse. 当HTTP状态为200时,此方法效果很好。但是当服务器返回一些HTTP错误(例如500-内部服务器错误,401-未授权错误等)时,我将收到JSON.parse的错误消息。 I am not sure how to handle errors for this.store.find() calls. 我不确定如何处理this.store.find()调用的错误。

Note: It returns Ember's promiseArray which I need to check once resolved (before actually assigning it to model). 注意:它返回Ember的promiseArray,解决后我需要检查它(在将其实际分配给模型之前)。 Any help on this topic would be much appreciated. 对此主题的任何帮助将不胜感激。 Thanks. 谢谢。

What about using the promise's catch callback to handle the errors? 使用promise的catch回调处理错误怎么办? Untested, but something like this should work: 未经测试,但类似这样的方法应该起作用:

self_controller.store.find('product').then(function(products) {
  self_controller.set('model', products);
}).catch(function(reason) {
  // Do something to handle the error response...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM