Giter Club home page Giter Club logo

errawr-rails's Introduction

Errawr::Rails

Raise and render errors in Rails using Errawr

Build Status Dependency Status Coverage Status Code Climate

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.

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.

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:

{
  "error": "bad_request",
  "description": "Bad Request"
}

Depending on what renderer is used additional metadata may be added to the response output:

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
{
  "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.

class MyCustomRenderer
  def call(error)
  {
    json: {
      hello: 'world'
    }
  }
  end
end
class SomeController < ApplicationController
  include Errawr::Rails.with_renderer(MyCustomRenderer)

  def index
    if params[:dont_work] == true
      error!(:bad_request)
    end
  end
end
{
  "hello": "world"
}

HTTP Status Codes

Errawr::Rails uses the Errawr::HTTP gem to add support for 4xx and 5xx 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

Sticksnleaves

Errawr::Rails is maintained and funded by Sticksnleaves

Thanks to all of our contributors

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.