How to handle a 400 error with Ember Data (2.4.0-canary)

巧了我就是萌 提交于 2019-12-12 15:42:33

问题


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?


回答1:


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"
   }
  ]
}


来源:https://stackoverflow.com/questions/34570160/how-to-handle-a-400-error-with-ember-data-2-4-0-canary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!