简体   繁体   English

如何在 Rails API 中返回自定义错误代码?

[英]How to return custom error codes in a Rails API?

Given a RESTful API, implemented in Rails, I want to include in the responses not only the errors messages, generated by ActiveModel::Validations , but also custom error codes.给定一个在 Rails 中实现的 RESTful API,我想在响应中不仅包含ActiveModel::Validations生成的错误消息,还包含自定义错误代码。 First of all I would like to point out that I am not talking about HTTP Status codes .首先,我想指出我不是在谈论HTTP 状态代码 I am talking about having an error of any type (from general errors like record not found to small validation errors like username can't be blank ) be mapped to a unique numeric code, that is custom application-specific error codes.我说的是将任何类型的错误( record not found类的一般错误到username can't be blank类的小验证错误)映射到唯一的数字代码,即自定义应用程序特定的错误代码。 Let me give an example - given a list of error codes, like:让我举个例子——给定一个错误代码列表,比如:

1: record not found
... some other errors
# Validation errors for User model between 1000 to 2000 
1001: first name can't be blank 
1002: first name must contain at least 3 characters 
1003: last name can't be blank 
1004: last name must contain at least 3 characters 
...some other errors

If I have a form for a user and submit it with empty fields for first and last name, I want to have in the response body something like:如果我有一个用户表单并提交它的名字和姓氏为空字段,我希望在响应正文中包含以下内容:

{error_codes: [1001, 1002, 1003, 1004]}

or something similar (for example I could have an array of error objects , each with a code, message for developer, message for user etc.).或类似的东西(例如,我可以有一个错误对象数组,每个对象都有一个代码、开发人员的消息、用户的消息等)。 Let me give an example with the Twilio API, taken from RESTful API Design: what about errors?让我举一个取自RESTful API Design的 Twilio API 示例:错误呢? :

在此处输入图片说明

Here, 20003 is some custom Twilio-specific code.这里,20003 是一些自定义的 Twilio 特定代码。 The question is - how can this be implemented in Rails?问题是 - 这如何在 Rails 中实现? I see several difficult aspects:我看到几个困难的方面:

  • how do I get a list of all possible errors that can be encountered.如何获取可能遇到的所有可能错误的列表。 It is hard to get such a list even only for the validation errors, let alone the other types of errors that can occur.即使仅针对验证错误也很难获得这样的列表,更不用说可能发生的其他类型的错误了。
  • how should this list be organized - maybe in a YAML file?这个列表应该如何组织 - 也许在一个 YAML 文件中?
  • how do I access the list - maybe something similar to the way translations are accessed via I18n.t ?我如何访问列表 - 也许类似于通过I18n.t访问翻译的方式?

I will really appreciate any advice on the topic.我将非常感谢有关该主题的任何建议。 Thank you.谢谢你。

PS I think this is a similar question . PS 我认为这是一个类似的问题

ActiveModel built-in validators can be found here .可以在此处找到 ActiveModel 内置验证器。 Sometimes one validator can check for more than one thing and output different messages.有时一个验证器可以检查不止一件事并输出不同的消息。 The easiest way to see them all is, as you've guessed, in its I18n yaml file, which can be found here .如您所料,查看它们的最简单方法是在其 I18n yaml 文件中,可以在此处找到。

One way of doing what you want is overwriting those messages with your custom codes.做你想做的一种方法是用你的自定义代码覆盖这些消息。 Another way is passing a custom message when explicitly attaching a validator to your models.另一种方法是在将验证器显式附加到模型时传递自定义消息。

validates :name, message: 'code:001 - my custom message'

Those two options won't help you with structure, though.不过,这两个选项对您的结构没有帮助。 You won't have a different key code on your json out of the box.开箱即用的 json 上不会有不同的密钥code

One way you can accomplish that is to can create a helper to parse the error messages and extract the codes after they have been assigned to a model instance.您可以实现的一种方法是创建一个帮助程序来解析错误消息并在将它们分配给模型实例后提取代码。 Something along the lines of:类似的东西:

def extract_error_codes(error_messages)
  error_messages.map{ |message| message.match('^code:(\d+)\s-')[1] }
end

That would give you an array of error codes for that instance if you'd used the format code:001 - my custom message .如果您使用格式code:001 - my custom message那将为您提供该实例的错误代码数组。

Another, much more involved way, is to tap into ActiveModel's Validator class and store an error code when a validation fails.另一种更复杂的方法是利用 ActiveModel 的 Validator 类并在验证失败时存储错误代码。 That would require going into each validator to assign the code.这需要进入每个验证器来分配代码。

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

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