Skip to content

Commit

Permalink
Updated README.
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Smith committed Nov 21, 2013
1 parent 6a723ed commit c0600d6
Showing 1 changed file with 105 additions and 3 deletions.
108 changes: 105 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Errawr::Rails

Errawr support for Rails
Raise and render errors in Rails using Errawr

[![Build Status](https://travis-ci.org/anthonator/errawr-rails.png?branch=master)](https://travis-ci.org/anthonator/errawr-rails) [![Coverage Status](https://coveralls.io/repos/anthonator/errawr-rails/badge.png?branch=master)](https://coveralls.io/r/anthonator/errawr-rails?branch=master)
[![Build Status](https://travis-ci.org/anthonator/errawr-rails.png?branch=master)](https://travis-ci.org/anthonator/errawr-rails) [![Coverage Status](https://coveralls.io/repos/anthonator/errawr-rails/badge.png?branch=master)](https://coveralls.io/r/anthonator/errawr-rails?branch=master) [![Code Climate](https://codeclimate.com/github/anthonator/errawr-rails.png)](https://codeclimate.com/github/anthonator/errawr-rails)

## Installation

Expand All @@ -20,7 +20,109 @@ Or install it yourself as:

## Usage

TODO: Write usage instructions here
### 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(Rails::Errawr::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(Rails::Errawr::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 ```Rails::Errawr::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

Expand Down

0 comments on commit c0600d6

Please sign in to comment.