我从后端收到JSON Api conform个错误:
{
"errors": [
{
"status": "400",
"source": {
"pointer": "/data/attributes/description"
},
"detail": "This field may not be null."
},
{
"status": "400",
"source": {
"pointer": "/data/attributes/due-date"
},
"detail": "This field may not be null."
},
{
"status": "400",
"source": {
"pointer": "/data/attributes/extra-comments"
},
"detail": "This field may not be null."
},
{
"status": "400",
"source": {
"pointer": "/data/attributes/name"
},
"detail": "This field may not be null."
},
{
"status": "400",
"source": {
"pointer": "/data/attributes/payment-type"
},
"detail": "This field may not be null."
},
{
"status": "400",
"source": {
"pointer": "/data/attributes/price"
},
"detail": "This field may not be null."
}
]
}
我尝试在我的模板中显示它们,如EmberData documentation:
中所述{{#each model.errors.messages as |message|}}
<div class="error">
{{message}}
</div>
{{/each}}
没有显示任何内容。我会说模型中的.errors
没有填充,但我不知道如何检查。我怎么能:
model.errors
?model.errors
?model
和所有属性?总的来说,我遇到的是Ember的新版本很难调试。每当我在控制台中显示任何Ember对象时,我只会看到一些Computed
属性,每当我试图查看它们时都不会扩展。
我的后端是:
这是我发布到后端的数据(JSONAPi符合):
{
"data": {
"attributes": {
"name": null,
"description": null,
"extra-comments": null,
"min-price": 30,
"max-price": 3000,
"price-step": 10,
"price": null,
"payment-type": null,
"due-date": null
},
"relationships": {
"seller": {
"data": null
},
"artist": {
"data": null
},
"subcategory": {
"data": null
}
},
"type": "projects"
}
}
后端可以正常使用,检测错误,并提供符合errors
的JSON APi回复,如上所述。
答案 0 :(得分:2)
我想我知道发生了什么(因为它也发生在我身上)。
将HTTP错误代码从400更改为422(不可处理的实体),并检查它是否解决了问题。
另外,查看JSONAPIAdapter的source code(从RestAdapter扩展),我认为我是对的。
isInvalid: function(status, headers, payload) {
return status === 422;
},
这可以更改为(adapters/application.js
):
import DS from 'ember-data';
import config from '../config/environment';
export default DS.JSONAPIAdapter.extend(DataAdapterMixin, {
host: config.API_HOST,
namespace: config.API_NAMESPACE,
isInvalid: function(status, headers, payload) {
return status === 400 || status === 422;
},
});
答案 1 :(得分:1)
作为参考,我在django方面做了这个:
from rest_framework_json_api.exceptions import exception_handler
def custom_exception_handler(exc, context):
# DRF returns 400, but EmberData wants 422. I will force a 422, always.
# Call the rest_framework_json_api's exception handler first,
# to get the standard error response.
response = exception_handler(exc, context)
# TODO: is this correct? 422 in all exception cases?!
response.status_code = 422
return response