Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在处理错误时在ember数据中设置自定义响应json_Javascript_Ember.js_Error Handling_Ember Data - Fatal编程技术网

Javascript 如何在处理错误时在ember数据中设置自定义响应json

Javascript 如何在处理错误时在ember数据中设置自定义响应json,javascript,ember.js,error-handling,ember-data,Javascript,Ember.js,Error Handling,Ember Data,我现在正在使用: ember:3.8.1 ember数据:3.10.0 我正试图从GrailsAPI发送一个错误响应,其中包含一些额外的数据。 它看起来像这样: respond( status: HttpStatus.UNPROCESSABLE_ENTITY, errors: errors additionalData: someMap ) response.status = HttpStatus.UNPROCESSABLE_ENTITY.value() ren

我现在正在使用:

  • ember:3.8.1
  • ember数据:3.10.0
我正试图从GrailsAPI发送一个错误响应,其中包含一些额外的数据。 它看起来像这样:

respond(
    status: HttpStatus.UNPROCESSABLE_ENTITY, 
    errors: errors
    additionalData: someMap
)

response.status = HttpStatus.UNPROCESSABLE_ENTITY.value()
render([errors: [[status: HttpStatus.UNPROCESSABLE_ENTITY,
                  title : e.message,
                  detail: message,
                  meta  : [additionalData: additionalData]]
]] as JSON)
在Ember的前端,我尝试用以下方式捕捉它:

object.save()。然后(函数(){
// (...)
}).catch((响应)=>{
//这里我想访问“response.additionalData”
// (...)
});
现在,我知道
ember data
有自己的方法来处理和绑定错误响应(),但是在
ember data:2.10.0
中,我能够在catch with
response.additionalData
中捕获和处理错误

在版本中,我使用的是
响应。附加数据
始终是
未定义的
,我无法以任何方式获取它。 它来自后端,因为我可以在浏览器开发工具中看到它的响应

如何在最新的
余烬数据中实现这一点?
我确实尝试过编写
适配器
并重写
handleResponse
函数,但即使是我自己的CustomErrorClass也与本机类似

任何帮助都将不胜感激。
提前谢谢

因此,我确实尝试了一些JSONAPI格式的
meta
成员选项,如下所述:

起初,这没有帮助,服务器给出的答案是:

Error: Assertion Failed: Your transport record was saved to the server, but the response does not have an id and no id has been set client side. Records must have ids. Please update the server response to provide an id in the response or generate the id on the client side either before saving the record or while normalizing the response.
然后我向
HttpServletResponse
对象添加了响应状态,如下所示:

response.status = HttpStatus.UNPROCESSABLE_ENTITY.value()
因此,完整响应部分如下所示:

respond(
    status: HttpStatus.UNPROCESSABLE_ENTITY, 
    errors: errors
    additionalData: someMap
)

response.status = HttpStatus.UNPROCESSABLE_ENTITY.value()
render([errors: [[status: HttpStatus.UNPROCESSABLE_ENTITY,
                  title : e.message,
                  detail: message,
                  meta  : [additionalData: additionalData]]
]] as JSON)
还要注意,
errors
内容在双括号中
[[
,JSONAPI格式才有效

有了这个,我终于得到了正确的答案,里面有
meta
成员: