简体   繁体   English

如何在Rails JSON API中呈现错误代码

[英]How to render error codes in Rails JSON API

I want to render error codes in my error responses as JSON API describes here . 我想在错误响应中呈现错误代码,如JSON API 此处所述。 I use ActiveInteraction gem and it uses ActiveModel::Errors under the hood for creating error objects. 我使用ActiveInteraction gem,它使用ActiveModel::Errors创建错误对象。 According to the documentation ActiveModel::Errors library just sets an attribute and a message if you want to add an error to your response object. 根据文档 ActiveModel::Errors库,如果您想向响应对象添加错误, ActiveModel::Errors设置一个属性和一条消息。

add(attribute, message = :invalid, options = {})

So how could I set a code property while adding an error? 那么如何在添加错误时设置code属性?

Here is an extract from the rails api docs: 以下是Rails api文档的摘录:

person.errors.add(:name, :not_implemented, message: "must be implemented")

Actually the ':not_implemented' is the code you need. 实际上,':not_implemented'是您需要的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM