显示 JSON Api 符合错误
Display JSON Api conform errors
我从后端收到 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
没有填充,但我不确定如何检查它。我怎样才能:
- 显示收到的ajax回复?
- 确保 Ember数据正在处理回复并填充
model.errors
?
- 在控制台显示处理后的
model.errors
?
- 显示
model
和所有属性?
总的来说,我发现 Ember 的新版本很难调试。每当我在控制台中显示任何 Ember 对象时,我只会看到一些 Computed
属性,每当我试图查看它们时,这些属性都不会展开。
我的后端是:
- Django 1.9
- 和django-rest-framework
- django-rest-framework-json-api
编辑
这是我向后端发布的数据(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"
}
}
后端对此没问题,检测到错误,并提供 JSON APi 符合 errors
的回复,如上所述。
我认为我知道发生了什么(因为它也发生在我身上)。
将 HTTP 错误代码从 400 更改为 422(无法处理的实体)并检查它是否解决了问题。
此外,查看 JSONAPIAdapter(从 RestAdapter 扩展而来)的 source code,我认为我是对的。
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;
},
});
作为参考,我在 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
我从后端收到 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
没有填充,但我不确定如何检查它。我怎样才能:
- 显示收到的ajax回复?
- 确保 Ember数据正在处理回复并填充
model.errors
? - 在控制台显示处理后的
model.errors
? - 显示
model
和所有属性?
总的来说,我发现 Ember 的新版本很难调试。每当我在控制台中显示任何 Ember 对象时,我只会看到一些 Computed
属性,每当我试图查看它们时,这些属性都不会展开。
我的后端是:
- Django 1.9
- 和django-rest-framework
- django-rest-framework-json-api
编辑
这是我向后端发布的数据(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"
}
}
后端对此没问题,检测到错误,并提供 JSON APi 符合 errors
的回复,如上所述。
我认为我知道发生了什么(因为它也发生在我身上)。
将 HTTP 错误代码从 400 更改为 422(无法处理的实体)并检查它是否解决了问题。
此外,查看 JSONAPIAdapter(从 RestAdapter 扩展而来)的 source code,我认为我是对的。
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;
},
});
作为参考,我在 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