如何呈现正确的JSON格式,并引发错误

我确信我在这里有一个基本的rescue_from问题,但是,当我的Validation失败时引发异常,但我似乎无法弄清楚,我试图获得期望的输出。

目前我的ApplicationController如下所示:

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

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

end

我得到的JSON输出是:

    {
  "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"
  ]
}

我不知道如何得到这个。 当我注释掉ApplicationControllerrescue_from方法并设置我的RecordController创建方法时,我得到了所需的输出:

  def create
    r = Record.create(record_params)
    r.save
    if r.save
    render json: r
    else
      render json: r.errors
    end
  end

虽然这是我想要的,但我必须去每个控制器并添加这个,但这不会是一个DRY方法......我宁愿将它集中在ApplicationController

任何帮助表示赞赏。 谢谢!

我检查了正确的JSON格式,以及如何在Ruby on Rails中“漂亮”地格式化我的JSON输出?


你可以这样做:

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的异常#消息http://ruby-doc.org/core-2.2.0/Exception.html#method-i-message以您描述的方式返回错误。 我通常会看到rescue_from被用来返回通用的404页面。

希望这可以帮助


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

查看上面的链接规范,因为可以使用错误对象返回几个不同的键

{
 "error": {
    "title": "RecordNotFound",
    "detail": "Record not found for id: 44. Maybe deleted?"
  }
}

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

该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?"
  }
}

我希望这个答案可以帮助别人! 干杯

链接地址: http://www.djcxy.com/p/47163.html

上一篇: How To Render Correct JSON format with raised error

下一篇: Pretty print a JSON object created in Ruby to Chrome browser console