https://github.com/vadimstroganov/error_responder
Error responder for Rails API
https://github.com/vadimstroganov/error_responder
error-handler error-handling rails rails-api
Last synced: 19 days ago
JSON representation
Error responder for Rails API
- Host: GitHub
- URL: https://github.com/vadimstroganov/error_responder
- Owner: vadimstroganov
- License: mit
- Created: 2017-08-01T09:49:42.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-08-03T13:39:48.000Z (over 8 years ago)
- Last Synced: 2025-10-05T10:32:43.317Z (5 months ago)
- Topics: error-handler, error-handling, rails, rails-api
- Language: Ruby
- Homepage:
- Size: 27.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
[](https://badge.fury.io/rb/error_responder)
### Error responder for Rails API
#### Generator of standard HTTP responses and error serializer for models.
### Getting Started
Add it to your Gemfile:
```ruby
gem 'error_responder'
```
#### 1) Generate standard HTTP responses
```ruby
err_respond(err_code, key: nil, message: nil)
```
#### 2) Generate responce with model validation errors
```ruby
serialize_errors(errors, options = {})
```
### Usage example
#### 1) Generate standard HTTP responses
```ruby
err_respond 404
```
Will be generated:
```json
{
"status": "404",
"info": "Not Found",
"errors": {}
}
```
You can pass a custom error message:
```ruby
err_respond 404, key: 'user', message: 'Not present in database.'
```
Will be generated:
```json
{
"status": "404",
"info": "Not Found",
"errors": {
"user": "Not present in database."
}
}
```
#### 2) Generate responce with model validation errors
```ruby
@user = User.new(user_params)
if @user.save
# ...
else
serialize_errors(@user.errors)
end
```
Will be generated (model errors):
```json
{
"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](http://opensource.org/licenses/MIT).