简体   繁体   English

ember-data API中的错误响应

[英]Error response in ember-data api

My code for saving document to API looks like that 我将文档保存到API的代码如下所示

    save(category) {
        category.save().then(() => {
            this.transitionTo('categories');
        }).catch((adapterError) => {
            console.log(category.get('errors').toArray());
            console.log(category.get('isValid'));
        });
    }, 

When API answers is: API的答案是:

{"errors":[{"attribute":"name","message":"This value should not be blank."}]} {“错误”:[{“属性”:“名称”,“消息”:“此值不能为空。”}]}

then 然后

category.get('isValid') category.get('isValid')

still returns true. 仍然返回true。

My question is, how validation errors should looks like? 我的问题是,验证错误应该是什么样子?

By default, ember-data's adapter determines that a response is invalid when the status code is 422. You can override the isInvalid function of the adapter to change this. 默认情况下,当状态码为422时,ember-data的适配器确定响应无效。您可以重写适配器的isInvalid函数来更改此设置。

Also, ember-data now expects errors to be formatted into a json-api error object. 此外,ember-data现在希望将错误格式化为json-api错误对象。 If your backend doesn't return it in this format, you can transform it in ember by overriding the handleResponse function of the adapter. 如果您的后端没有以这种格式返回它,则可以通过覆盖适配器的handleResponse函数在ember中对其进行转换。

This is a sample json-api error: 这是一个示例json-api错误:

{"errors": [
  {
    "detail": "Must be unique",
    "source": { pointer: "/data/attributes/title"}
  },
  {
    "detail": "Must not be blank",
    "source": { pointer: "/data/attributes/content"}
  }
]}

If you're returning error responses you described above, you would have to do something like this in your adapter: 如果返回上面描述的错误响应,则必须在适配器中执行以下操作:

handleResponse(status, headers, payload) {                                                                                                                                                                                               
  if (status === 422 && payload.errors) {                                                                                                                                                                                                
    let jsonApiErrors = [];                                                                                                                                                                                                              

    for (let key in payload.errors) {                                                                                                                                                                                                    
      for (let i = 0; i < payload.errors[key].length; i++) {                                                                                                                                                                             
        jsonApiErrors.push({                                                                                                                                                                                                             
          detail: payload.errors[key][i],                                                                                                                                                                                                
          source: {                                                                                                                                                                                                                      
            pointer: `data/attributes/${key}`                                                                                                                                                                                            
          }                                                                                                                                                                                                                              
        });                                                                                                                                                                                                                              
      }                                                                                                                                                                                                                                  
    }                                                                                                                                                                                                                                    

    return new DS.InvalidError(jsonApiErrors);                                                                                                                                                                                           
  } else {                                                                                                                                                                                                                               
    return this._super(...arguments);                                                                                                                                                                                                    
  }                                                                                                                                                                                                                                      
}

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

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