错误未填充

时间:2016-01-04 16:55:10

标签: django ember.js ember-data django-rest-framework json-api

我从后端收到错误:

{
  "errors": {
    "extra_comments": [
      "This field may not be null."
    ],
    "name": [
      "This field may not be null."
    ],
    "due_date": [
      "This field may not be null."
    ],
    "price": [
      "This field may not be null."
    ],
    "payment_type": [
      "This field may not be null."
    ],
    "description": [
      "This field may not be null."
    ]
  }
}

我尝试在我的模板中显示它们:

{{#each model.errors.messages as |message|}}
  <div class="error">
    {{message}}
  </div>
{{/each}}

没有显示任何内容。 EmberData有问题吗? Ember模板语法更改的问题? Ember有问题吗?适配器?我的后端?不知道。问题表面太大了。我怎么能:

  • 显示收到的ajax回复?
  • 确保EmberData正在处理回复并填充model.errors
  • 在控制台中显示已处理的model.errors

总的来说,我遇到的是Ember的新版本很难调试。每当我在控制台中显示任何Ember对象时,我只会看到一些Computed属性,每当我试图查看它们时都不会计算它们。

修改

我的后端是:

我不确定django-rest-framework-json-api是否能够返回JSON Api符合错误。我打开了issue

2 个答案:

答案 0 :(得分:2)

您从后端收到的错误并非JSON API符合errors
您必须在自定义序列化程序的extractErrors方法中转换错误(有关示例,请参阅RESTSerializer文档),或者更改后端以返回符合JSON API的错误。

multiple errors符合JSON API规范的示例:

{
  "errors": [
    {
      "status": "403",
      "source": { "pointer": "/data/attributes/secret-powers" },
      "detail": "Editing secret powers is not authorized on Sundays."
    },
    {
      "status": "422",
      "source": { "pointer": "/data/attributes/volume" },
      "detail": "Volume does not, in fact, go to 11."
    },
    {
      "status": "500",
      "source": { "pointer": "/data/attributes/reputation" },
      "title": "The backend responded with an error",
      "detail": "Reputation service not responding after three requests."
    }
  ]
}

答案 1 :(得分:0)

在代码I found上查看django端需要此设置:

REST_FRAMEWORK = {
    ...
    'EXCEPTION_HANDLER': 'rest_framework_json_api.exceptions.exception_handler',
    ...
}