No commit activity in last 3 years
No release in over 3 years
Convenient return errors to the API as JSON
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.11
~> 10.0
 Project Readme

JsonErrorSerializer

This gem provides a simple way to respond API errors messages with correct HTTP responces from Ruby On Rails applications.

Installation

Add this line to your application's Gemfile:

gem 'json_error_serializer'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install json_error_serializer

Usage

In your controller:

respond_404
respond_404('User not found')

Result: HTTP 404 (Not found)

{
  "status": "404",
  "message": "Not found"
}
{
  "status": "404",
  "message": "User not found"
}

Also possible to use in model validation errors:

@user = User.new(user_params)
if @user.save
    # render your OK responce
else
    validate_errors_serialize(@user.errors)
end

Result(example): HTTP 409 (Conflict)

{
  "errors": [
    {
      "id": "email",
      "title": "Email has already been taken."
    },
    {
      "id": "telephone",
      "title": "Telephone has already been taken."
    }
  ]
}

Available methods refer to the wiki

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Strollager/error_json_serializer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.