如何使用Ember Data(2.4.0-canary)处理400错误 [英] How to handle a 400 error with Ember Data (2.4.0-canary)

查看:129
本文介绍了如何使用Ember Data(2.4.0-canary)处理400错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我的问题与我的查询或我的回复或其他问题有关。



在我的路线上,我使用的是一个 queryRecord

  model(params){
return this.store .queryRecord('invite',{
'invitation_token':params.invitationToken
});
},

我在服务器端正确接收,但我正在测试邀请令牌不再存在的情况。因此,服务器返回一个具有json有效负载的400,这有一个错误的解释。

  {error:错误查找邀请} 

但是回到余烬边我只是得到两个错误只是一个)。

 处理路由时出错:accept-invitation Ember数据请求GET / api / users / invitation返回400 
错误:Ember数据请求GET / api / users / invite返回400

(在Ember 2.2.1下):

 处理路由时出错:accept-invitation适配器操作失败错误:适配器操作失败
错误:适配器操作失败

我应该从服务器返回没有找到令牌的情况?这些消息使它看起来像在Ember中发生了意想不到的事情。



我会假设一个400会告诉Ember有一个错误,它会转到错误状态为路线。但是,它会在应用程序级别处于错误状态,并在日志中吐出这些错误消息。



我希望服务器返回400,所以我不知道为什么Ember抱怨。我应该怎么回来呢,我应该如何正确处理?

解决方案

我相信你的应用程序应该返回一个响应格式不同。根据 JSON API规范


错误对象必须作为JSON API文档顶部
级别中的错误键入的数组返回。


该规范还指定可以提供以下成员(请参阅完整列表的链接)。


标题:一个简短的,人性化的概述,即不应该
的问题从发生到发生的问题发生变化,但
的本地化目的除外。



细节:一个人类可读的解释
特定于这个问题的发生。像标题一样,此字段的
值可以本地化



状态:适用于此问题的HTTP状态代码,表示为
a字符串值。


如果您尝试返回一些沿着下面的内容,工具如何运行?

  {
errors:[
{
status:400,
title:查找邀请错误
}
]
}


I can't figure out if my problem has to do with my query or my response or something else.

In my route I'm using a queryRecord:

model(params) {
  return this.store.queryRecord('invitation', { 
    'invitation_token' : params.invitationToken 
  });
},

I'm properly receiving on the server side but I'm testing the case where the invitation token no longer exists. Therefore, the server is returning a 400 with a json payload which has an explanation of the error.

{"error":"Error finding invitation"}

But back on the ember side I'm just getting two errors (really it is just one).

Error while processing route: accept-invitation Ember Data Request GET /api/users/invitation returned a 400
Error: Ember Data Request GET /api/users/invitation returned a 400

It used to be (under Ember 2.2.1):

Error while processing route: accept-invitation Adapter operation failed Error: Adapter operation failed
Error: Adapter operation failed

What am I supposed to be returning from the server in the case that the token wasn't found? These messages makes it looks like something unexpected happen within Ember.

I'd assume that a 400 would tell Ember that there was an error, and it would go to the error state for the route. But instead it goes to the error state at the application level and spits out these error messages in the log.

I'm expecting the server to return a 400, so I'm not sure why Ember is complaining. What should I be returning and how should I properly handle this?

解决方案

I believe that your application should be returning a response that is formatted differently. As per the JSON API specification,

Error objects MUST be returned as an array keyed by errors in the top level of a JSON API document.

The specification also specifies that the following members, among others, may be provided (see the link for the full list).

title: a short, human-readable summary of the problem that SHOULD NOT change from occurrence to occurrence of the problem, except for purposes of localization.

detail: a human-readable explanation specific to this occurrence of the problem. Like title, this field's value can be localized

status: the HTTP status code applicable to this problem, expressed as a string value.

If you try returning something along the lines of what is below, how does the tooling behave?

{
  "errors": [
   {
    "status": "400",
    "title": "Error finding invitation"
   }
  ]
}

这篇关于如何使用Ember Data(2.4.0-canary)处理400错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆