2

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. First of all I would like to point out that I am not talking about HTTP Status codes. 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. 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?:

enter image description here

Here, 20003 is some custom Twilio-specific code. The question is - how can this be implemented in 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?
  • how do I access the list - maybe something similar to the way translations are accessed via I18n.t?

I will really appreciate any advice on the topic. Thank you.

P.S. I think this is a similar question.

1 Answer 1

1

ActiveModel built-in validators can be found here. 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.

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.

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.

Another, much more involved way, is to tap into ActiveModel's Validator class and store an error code when a validation fails. That would require going into each validator to assign the code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.