Error responder for Rails API
Generator of standard HTTP responses and error serializer for models.
Getting Started
Add it to your Gemfile:
gem 'error_responder'
1) Generate standard HTTP responses
err_respond(err_code, key: nil, message: nil)
2) Generate responce with model validation errors
serialize_errors(errors, options = {})
Usage example
1) Generate standard HTTP responses
err_respond 404
Will be generated:
{
"status": "404",
"info": "Not Found",
"errors": {}
}
You can pass a custom error message:
err_respond 404, key: 'user', message: 'Not present in database.'
Will be generated:
{
"status": "404",
"info": "Not Found",
"errors": {
"user": "Not present in database."
}
}
2) Generate responce with model validation errors
@user = User.new(user_params)
if @user.save
# ...
else
serialize_errors(@user.errors)
end
Will be generated (model errors):
{
"status": 409,
"info": "Conflict",
"errors": {
"username": "Username can't be blank.",
"first_name": "First name can't be blank.",
"last_name": "Last name can't be blank."
}
}
License
The gem is available as open source under the terms of the MIT License.