Ruby 如何使用引发的错误呈现正确的JSON格式
我确信我这里有一个基本的Ruby 如何使用引发的错误呈现正确的JSON格式,ruby,json,ruby-on-rails-4,Ruby,Json,Ruby On Rails 4,我确信我这里有一个基本的rescue\u问题,但是,当我的验证失败时出现异常时,我试图获得所需的输出,但我似乎无法找到它 当前我的ApplicationController如下所示: class ApplicationController < ActionController::API include ActionController::Serialization rescue_from ActiveRecord::RecordInvalid do |e| render js
rescue\u问题,但是,当我的验证失败时出现异常时,我试图获得所需的输出,但我似乎无法找到它
当前我的ApplicationController
如下所示:
class ApplicationController < ActionController::API
include ActionController::Serialization
rescue_from ActiveRecord::RecordInvalid do |e|
render json: {error: e.message}, status: 404
end
end
{
"organization_id": [
"can't be blank"
],
"description": [
"can't be blank"
],
"artwork": [
"can't be blank"
],
"language_code": [
"can't be blank"
],
"copyright": [
"can't be blank"
],
"right_to_left": [
"is not included in the list"
],
"digital_audio": [
"is not included in the list"
],
"portion": [
"is not included in the list"
],
"electronic_text": [
"is not included in the list"
],
"old": [
"is not included in the list"
],
"new": [
"is not included in the list"
]
}
我想要得到的输出(或类似的输出)如下所示:
class ApplicationController < ActionController::API
include ActionController::Serialization
rescue_from ActiveRecord::RecordInvalid do |e|
render json: {error: e.message}, status: 404
end
end
{
"organization_id": [
"can't be blank"
],
"description": [
"can't be blank"
],
"artwork": [
"can't be blank"
],
"language_code": [
"can't be blank"
],
"copyright": [
"can't be blank"
],
"right_to_left": [
"is not included in the list"
],
"digital_audio": [
"is not included in the list"
],
"portion": [
"is not included in the list"
],
"electronic_text": [
"is not included in the list"
],
"old": [
"is not included in the list"
],
"new": [
"is not included in the list"
]
}
我不知道怎样才能得到这个。当我在ApplicationController
中注释掉rescue\u from
方法并设置myRecordController
创建方法时,我得到了所需的输出,如下所示:
def create
r = Record.create(record_params)
r.save
if r.save
render json: r
else
render json: r.errors
end
end
虽然这是我想要的,我必须去每一个控制器,并添加这个,但这不是一个干燥的方法。。。我宁愿将其集中在ApplicationController
感谢您的帮助。
谢谢
我已经检查了&您可以这样做:
def create
r = Record.create(record_params)
if r.save
render json: r.to_json
else
render json: errors: r.errors, status: 422
end
end
如果返回and error,它将返回以下内容:
{
errors:
{
attr_1:["can't be blank"],
attr_2:["can't be blank"]
}
}
与您展示的第一个示例相关,ruby的Exception消息以您描述的方式返回错误。我通常看到rescue\u from
被用来返回通用404页面
希望这能有所帮助经过更多的研究,我找到了答案
异常对象e
在主体的rescue\u中有一个记录
属性和导致异常的记录,您可以使用该记录的错误
属性来提取错误,并以您想要的格式创建响应:
我在ApplicationController
中将这一行编辑为:
rescue_from ActiveRecord::RecordInvalid do |e|
render json: {error: {RecordInvalid: e.record.errors}}, status: 406
end
将RecordController的创建方法更改为:
def create
r = Record.create!(record_params)
render json: r
end
这将给出输出:
{
"error": {
"RecordInvalid": {
"organization_id": [
"can't be blank"
],
"name": [
"can't be blank"
],
"description": [
"can't be blank"
],
"artwork": [
"can't be blank"
],
"language_code": [
"can't be blank"
],
"copyright": [
"can't be blank"
],
"right_to_left": [
"is not included in the list"
],
"digital_audio": [
"is not included in the list"
],
"portion": [
"is not included in the list"
],
"electronic_text": [
"is not included in the list"
],
"old": [
"is not included in the list"
],
"new": [
"is not included in the list"
]
}
}
}
如果引发了其他异常,并且希望呈现示例,这将非常有帮助:
rescue_from ActiveRecord::RecordNotFound do
render json: {error: {RecordNotFound: "Record not found for id: #{params[:id]}. Maybe deleted?"}}, status: 404
end
使用此选项,如果我搜索无效记录44
,它将呈现:
{
"error": {
"RecordNotFound": "Record not found for id: 44. Maybe deleted?"
}
}
我希望这个答案能帮助别人!干杯只是更新,接受的答案不是错误说明
查看上面链接的规范,因为有几个不同的键可以随错误对象一起返回
{
"error": {
"title": "RecordNotFound",
"detail": "Record not found for id: 44. Maybe deleted?"
}
}
我正试图避免自己必须向每个控制器和操作添加代码。用你的方法,我必须这样做。这就是我只想将代码添加到ApplicationController
的原因。是的,在不尝试解析异常消息的情况下,我不确定如何获得与对象本身返回的格式相同的格式。