https://github.com/anthonator/errawr-rails
Raise and render errors in Rails using Errawr
https://github.com/anthonator/errawr-rails
Last synced: 6 days ago
JSON representation
Raise and render errors in Rails using Errawr
- Host: GitHub
- URL: https://github.com/anthonator/errawr-rails
- Owner: anthonator
- License: mit
- Created: 2013-11-20T23:40:34.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2015-07-18T05:58:04.000Z (almost 11 years ago)
- Last Synced: 2026-06-13T00:09:06.698Z (23 days ago)
- Language: Ruby
- Homepage:
- Size: 211 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Errawr::Rails
Raise and render errors in Rails using Errawr
[](https://travis-ci.org/anthonator/errawr-rails) [](https://gemnasium.com/anthonator/errawr-rails) [](https://coveralls.io/r/anthonator/errawr-rails?branch=master) [](https://codeclimate.com/github/anthonator/errawr-rails)
## Installation
Add this line to your application's Gemfile:
gem 'errawr-rails'
And then execute:
$ bundle
Or install it yourself as:
$ gem install errawr-rails
## Usage
### Getting Started
To start raising errors in Rails just include ```Errawr::Rails``` in a controller. This will provide access to the ```#error!``` method in your controller.
```ruby
class SomeController < ApplicationController
include Errawr::Rails
def index
if params[:dont_work] == true
error!(:bad_request)
end
end
end
```
### Rendering Error Responses
If you'd like to catch and render errors in a particular format include ```Errawr::Rails``` using the ```#with_renderer``` method.
```ruby
class SomeController < ApplicationController
include Errawr::Rails.with_renderer(Errawr::Rails::Renderers::JSON)
def index
if params[:dont_work] == true
error!(:bad_request)
end
end
end
```
The above example will render the error as JSON using the following format:
```json
{
"error": "bad_request",
"description": "Bad Request"
}
```
Depending on what renderer is used additional metadata may be added to the response output:
```ruby
class SomeController < ApplicationController
include Errawr::Rails.with_renderer(Errawr::Rails::Renderers::JSON)
def index
if params[:dont_work] == true
error!(:bad_request, metadata: { extra_info: 'I like candy' })
end
end
end
```
```json
{
"error": "bad_request",
"description": "Bad Request",
"extra_info": "I like candy"
}
```
Currently the only renderer that ships with Errawr::Rails is ```Errawr::Rails::Renderers::JSON```.
### Custom Renderers
To create a custom renderer simple create a class with that specifies a ```call``` method that accepts a single parameter. The method can return anything that the Rails ```render``` method will accept.
```ruby
class MyCustomRenderer
def call(error)
{
json: {
hello: 'world'
}
}
end
end
```
```ruby
class SomeController < ApplicationController
include Errawr::Rails.with_renderer(MyCustomRenderer)
def index
if params[:dont_work] == true
error!(:bad_request)
end
end
end
```
```json
{
"hello": "world"
}
```
### HTTP Status Codes
Errawr::Rails uses the [Errawr::HTTP](https://github.com/anthonator/errawr-http) gem to add support for [4xx](https://github.com/anthonator/errawr-http#supported-4xx-status-codes) and [5xx](https://github.com/anthonator/errawr-http#supported-5xx-status-codes) HTTP status code errors.
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
## Credits
[](http://www.sticksnleaves.com)
Errawr::Rails is maintained and funded by [Sticksnleaves](http://www.sticksnleaves.com)
Thanks to all of our [contributors](https://github.com/anthonator/errawr-rails/graphs/contributors)