Jump to content

The ultimate community for Ruby on Rails developers.


REST JSON API and validation errors

rails rest json

  • Please log in to reply
2 replies to this topic

#1 inish777



  • Members
  • 2 posts

Posted 06 December 2013 - 05:15 PM

Hi all!

So, I want to made REST JSON API for my web app. It will be used by mobile apps, other web apps, etc. I have problem with ActiveRecord validation errors - it seems like they have no error codes, only messages. Of course, in case of error, I can return 422 HTTP status + error messages in JSON response body, but it seems to be better, if every error will have its own error code (50 for blank user name, 51 for blank password, etc) which will be returned in response body. Is it possible to implement in Rails or you can suggest other ways of error reporting?

#2 james



  • Members
  • 223 posts
  • LocationLeeds, U.K.

Posted 07 December 2013 - 10:54 AM   Best Answer

api's should stick to standard http protocols so stick with the 4xx and 5xx error codes.

Why not include a status code with the error message? That can then be interpreted by the user of the api? Custom error messages are not difficult to implement just add them to the erros object of the model instance. You can add multiple errors.


You can just push them


errors[:base] << "50"


There is even no reason why the error couldn't be a hash instead of just a string


errors[:base] << "{code: 50}"

untested so you might need to play with that a bit.



  • inish777 likes this
Programming is just about problem solving!

#3 inish777



  • Members
  • 2 posts

Posted 07 December 2013 - 01:00 PM


Also tagged with one or more of these keywords: rails, rest, json

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users