如何使用引发的错误呈现正确的JSON格式

时间:2015-11-14 02:52:21

标签: ruby json ruby-on-rails-4

我确定我在这里有一个基本的param: foo ./test.sh: line 11: throw_error: command not found foo failed with code: 127 param: bar bar is fine. done 问题但是,当我的rescue_from失败但是我无法提出异常时,我正试图获得所需的输出似乎想通了。

目前我的Validation如下:

ApplicationController

我获得的JSON输出是:

class ApplicationController < ActionController::API
  include ActionController::Serialization

  rescue_from ActiveRecord::RecordInvalid do |e|
  render json: {error: e.message}, status: 404
  end  

end

我想得到的(或类似的)所需的输出如下:

    {
  "error": "Validation failed: Organization 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"
}

我不知道怎么做到这一点。当我在 { "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" ] } 中注释掉rescue_from方法并设置我的ApplicationController创建方法时,我得到了所需的输出:

RecordController

虽然这是我想要的,但我必须去每个控制器并添加它,但这不是一个干燥的方法......我宁愿将其集中在 def create r = Record.create(record_params) r.save if r.save render json: r else render json: r.errors end end

感谢任何帮助。 谢谢!

我查了Correct JSON format&amp; How can I “Pretty” format my JSON output in Ruby on Rails?

3 个答案:

答案 0 :(得分:0)

你可以这样做:

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

如果返回错误,则返回错误:

{
  errors:
    {
      attr_1:["can't be blank"],
      attr_2:["can't be blank"]
    }
}

与您展示的第一个示例相关,ruby的Exception#message http://ruby-doc.org/core-2.2.0/Exception.html#method-i-message以您描述的方式返回错误。我通常会看到rescue_from用于返回通用404页面。

希望这有帮助

答案 1 :(得分:0)

只是更新,接受的答案不是JSON-API错误规范。

查看上面的链接规范,因为可以使用错误对象

返回一些不同的键
{
 "error": {
    "title": "RecordNotFound",
    "detail": "Record not found for id: 44. Maybe deleted?"
  }
}

答案 2 :(得分:-1)

经过更多的研究,我发现了它。

该rescue_from正文中的异常对象e具有record属性以及导致异常的记录,您可以使用该记录的errors属性来提取错误并创建您想要的格式的回复:

我在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?"
  }
}

我希望这个答案可以帮助别人!欢呼声