• Stars
    star
    527
  • Rank 84,091 (Top 2 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 10 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple and safe way to dynamically render error pages or JSON responses for Rails apps

Rambulance build Gem Version

A simple and safe way to dynamically render error pages for Rails apps.

Features

Simple and Safe

Rambulance's exceptions app is simple, skinny and well-tested. It inherits from ActionController::Base, so it works fine even if your ApplicationController has an issue.

Flexible

You have full control of which error page to show for a specific exception. It also json rendering (perfect for API apps). It even provides a way to create a custom exceptions app.

Easy installation and development

You don't have to configure things that every single person has to do and Rambulance does everything for you.

Installation and Usage

Add this line to your application's Gemfile:

gem 'rambulance'

And then execute:

$ rails g rambulance:install

Rambulance's generator can only generate erb templates. If you want to use haml or slim templates, please see How to Convert Your .erb to .slim or html2haml.

Now you can start editing templates like app/views/errors/not_found.html.erb. Edit, run rails server and open localhost:3000/rambulance/not_found!

Setting Pairs of Exceptions and HTTP Statuses

Open config/initializers/rambulance.rb and to configure the list of pairs of exception/corresponding http status. For example, if you want to display:

  • 422(unprocessable entity) for ActiveRecord::RecordNotUnique
  • 403(forbidden) for CanCan::AccessDenied
  • 404(not found) for YourCustomException

Then do the following:

# config/initializers/rambulance.rb
config.rescue_responses = {
  "ActiveRecord::RecordNotUnique" => :unprocessable_entity,
  "CanCan::AccessDenied"          => :forbidden,
  "YourCustomException"           => :not_found
}

Local Development

Open localhost:3000/rambulance/*** in Your Browser

Just open one of the error pages via Rambulance:

This is useful when you want to edit templates without changing Rails configuration.

Set consider_all_requests_local to false

Change config.consider_all_requests_local to false in config/environments/development.rb.

config.consider_all_requests_local = false

This simulates how your production app displays error pages so you can actually raise an exception in your app and see how it works. Don't forget to change consider_all_requests_local back to true after you tried this strategy.

Custom Exceptions App

If you want to do some more things in a exceptions app, you can also write your own custom exceptions app:

$ rails g rambulance:exceptions_app

It will generate your own custom exceptions app. You can use whatever techniques you want to use in controllers like before_filter and flash[:notice] = "message..." since it's a grandchild of ActionController::Base!

Heavily customizing the exceptions app is strongly discouraged as there would be no guard against bugs that occur in the exceptions app.

Testing

Rambulance ships with a test helper that allows you to test an error page generated by Rails. All you have to do is to include Rambulance::TestHelper and you will be able to use the with_exceptions_app DSDL:

Rspec:

include Rambulance::TestHelper

it "shows an error page" do
  with_exceptions_app do
    get '/does_not_exist'
  end

  assert_equal 404, response.status
end

Minitest:

include Rambulance::TestHelper

test "it shows an error page" do
  with_exceptions_app do
    get '/does_not_exist'
  end

  assert_equal 404, response.status
end

Note that testing error pages is not encouraged in Rails as it leads to overuse of the rescue_from DSL in controllers.

Supported Versions

  • Ruby 2.3, 2,4, 2,5, 2.6, 2.7, 3.0, 3.1, and JRuby 9.3
  • Rails 4.2, 5.0, 5.1, 5.2, 6.0, 6.1, 7.0 and edge

Rambulance doesn't work with Rails 3.1 and below since they don't provide a way to use a custom exceptions app.

Contributing

  1. Fork it ( https://github.com/yuki24/rambulance/fork )
  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 a new Pull Request

License

Copyright (c) 2014-2015 Yuki Nishijima. See LICENSE.txt for further details.

More Repositories

1

artemis

Ruby GraphQL client on Rails that actually makes you more productive
Ruby
207
star
2

andpush

Android Push Notification in Ruby: The fastest client for FCM (Firebase Cloud Messaging)
Ruby
82
star
3

shoelace-rails

Rails helpers for https://shoelace.style, the design system.
Ruby
47
star
4

pushing

Finally, push notification framework that does not hurt. currently supports Android (FCM) and iOS (APNs)
Ruby
46
star
5

android-exercises

A collection of android projects I've done to learn android development.
Java
29
star
6

to_jbuilder

Convert JSON to jbuilder templates.
Ruby
15
star
7

capybara-shadowdom

Shadow DOM support for Capybara
Ruby
14
star
8

stimulus-lit

HTML rendering with `lit-html` for Stimulus Controllers.
TypeScript
13
star
9

did_you_mean-activerecord

More 'Did you mean?' experience on Rails.
Ruby
13
star
10

texting

SMS/MMS framework for Rails that doesn't hurt
Ruby
8
star
11

object_mapper

Convert JSON to Plain Old Ruby Objects
Ruby
6
star
12

friendable

Redis-Backed Friendship Engine for AR Models
Ruby
3
star
13

activerecord-bigquery-adapter

Ruby
2
star
14

hirameki

Redis-based one time token generator
Ruby
2
star
15

newrelic-httpclient

NewRelic instrumentation for HttpClient.
2
star
16

stenciljs-gem

Useful helpers, tasks, and generators For Stencil.js on Rails/Rack
Ruby
2
star
17

shoelace-rails-example

Ruby
2
star
18

fried_tofu

Scalatra-based image processing interface that is intended to use S3 and Heroku.
Scala
2
star
19

kodachi

Sinatra-based image processing interface that is intended to use S3 and Heroku.
Ruby
2
star
20

gh-link-status

JavaScript
2
star
21

rightchoice

always make the right choice!
Ruby
1
star
22

oven

API client generator
Ruby
1
star
23

emacs.el

My emacs lisp scripts.
Emacs Lisp
1
star
24

twilio-call-tracking-exampleapp

Ruby
1
star
25

heart_drawn_on_js

Heart drawn on javascript.
JavaScript
1
star
26

styleguide

JavaScript
1
star