• Stars
    star
    244
  • Rank 165,885 (Top 4 %)
  • Language
    Ruby
  • License
    BSD 3-Clause "New...
  • Created over 11 years ago
  • Updated over 6 years ago

Reviews

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

Repository Details

💥 Gaffe handles Rails error pages in a clean, simple way.

gaffe
Gaffe makes having customized error pages in Rails applications an easy thing.
It takes advantage of a feature present in Rails 3.2 (and 4.0+, obviously) called exceptions_app.


It comes with default error pages but makes it very easy to override them (which you should do). The default error pages look like this:

Installation

Add this line to your application’s Gemfile:

gem 'gaffe'

Usage

The easiest way to use Gaffe is with an initializer:

# config/initializers/gaffe.rb
Gaffe.enable!

Custom controller

However, if you want to use your own controller:

# config/initializers/gaffe.rb
Gaffe.configure do |config|
  config.errors_controller = 'ErrorsController'
end

Gaffe.enable!

It’s also possible to use a custom controller based on the URL in which the error has occured. Both absolute and relative URL supported. This is especially useful if you have an application that also serves API requests via JSON. You would probably want to serve API errors through JSON and regular errors through HTML pages.

# config/initializers/gaffe.rb
Gaffe.configure do |config|
  config.errors_controller = {
    %r[^/api/] => 'Api::ErrorsController',
    %r[^/] => 'ErrorsController',
    %r[^www.example.com] => 'HostSpecificErrorsController'
  }
end

Gaffe.enable!

The only required thing to do in your custom controller is to include the Gaffe::Errors module.

Only show will be called so you might want to override it. If you don’t override it, Gaffe will try to render the view "errors/#{@rescue_response}" within your application (or use its default error page if the view doesn’t exist).

You might also want to get rid of filters and other stuff to make sure that error pages are always accessible.

class ErrorsController < ApplicationController
  include Gaffe::Errors

  # Make sure anonymous users can see the page
  skip_before_action :authenticate_user!

  # Override 'error' layout
  layout 'application'

  # Render the correct template based on the exception “standard” code.
  # Eg. For a 404 error, the `errors/not_found` template will be rendered.
  def show
    # Here, the `@exception` variable contains the original raised error
    render "errors/#{@rescue_response}", status: @status_code
  end
end

For example, you might want your API::ErrorsController to return a standard JSON response:

class API::ErrorsController < API::ApplicationController
  include Gaffe::Errors

  # Make sure anonymous users can see the page
  skip_before_action :authenticate_user!

  # Disable layout (your `API::ApplicationController` probably does this already)
  layout false

  # Render a simple JSON response containing the error “standard” code
  # plus the exception name and backtrace if we’re in development.
  def show
    output = { error: @rescue_response }
    output.merge! exception: @exception.inspect, backtrace: @exception.backtrace.first(10) if Rails.env.development? || Rails.env.test?
    render json: output, status: @status_code
  end
end

Custom views

You can (and should!) also use your own views. You just have to create a layout:

<!-- app/views/layouts/error.html.erb -->
<h1>Error!</h1>
<%= yield %>

And create a different view for each possible error rescue response (rails reference). For example, for 404 errors:

<!-- app/views/errors/not_found.html.erb -->
<p>This page does not exist.</p>

Custom exceptions

If your application is raising custom exceptions (through gems or your code) and you want to render specific views when it happens, you can map them to specific rescue responses.

# config/application.rb
config.action_dispatch.rescue_responses.merge! 'CanCan::AccessDenied' => :forbidden
config.action_dispatch.rescue_responses.merge! 'MyCustomException' => :not_acceptable

Rails development environment

Rails prefers to render its own debug-friendly errors in the development environment, which is totally understandable. However, if you want to test Gaffe’s behavior in development you’ll have to edit the config/environments/development.rb file.

# Make Rails use `exceptions_app` in development
config.consider_all_requests_local = false

Rails test environment

You also have to configure Rails’ test environment so it lets Gaffe handle exceptions in request tests. You’ll have to edit the config/environments/test.rb file.

# Make Rails use `exceptions_app` in tests
config.consider_all_requests_local = false

# Render exceptions instead of raising them
config.action_dispatch.show_exceptions = true

Unfortunately, controller tests (called functional tests in Rails) do not work with Gaffe, since they only test method calls in the controller class — they do not go through the entire Rack stack to simulate a real HTTP request.

To test responses sent by Gaffe, you must use request tests.

Contributors

License

Gaffe is © 2013-2016 Mirego and may be freely distributed under the New BSD license. See the LICENSE.md file.

The mushroom cloud logo is based on this lovely icon by Gokce Ozan, from The Noun Project. Used under a Creative Commons BY 3.0 license.

About Mirego

Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We're a team of talented people who imagine and build beautiful Web and mobile applications. We come together to share ideas and change the world.

We also love open-source software and we try to give back to the community as much as we can.

More Repositories

1

accent

The first developer-oriented translation tool. True asynchronous flow between translators and your team.
Elixir
1,257
star
2

elixir-boilerplate

⚗ The stable base upon which we build our Elixir projects at Mirego.
Elixir
1,124
star
3

activerecord_json_validator

🔩 ActiveRecord::JSONValidator makes it easy to validate JSON attributes against a JSON schema.
Ruby
383
star
4

mix_audit

🕵️‍♀️ MixAudit provides a mix deps.audit task to scan a project Mix dependencies for known Elixir security vulnerabilities
Elixir
324
star
5

telemetry_ui

Telemetry based metrics UI. Take your telemetry metrics and display them in a web page.
Elixir
185
star
6

elixir-security-advisories

🛡 Public database of Elixir security advisories pulled from GitHub Advisory Database
Elixir
119
star
7

absinthe_error_payload

Bridges the gap between Ecto and Absinthe for mutation payload
Elixir
116
star
8

trikot

🧶 Trikot is a framework that helps building Kotlin Multiplatform apps. iOS, Android and Web are the primary targets.
Kotlin
96
star
9

credo_naming

🏷 A suite of Credo checks to enforce naming best practices in an Elixir project
Elixir
88
star
10

MCUIViewLayout

MCUIViewLayout is a category added over UIView to easily build layouts
Objective-C
77
star
11

react-boilerplate

⚛ The stable base upon which we build our React projects at Mirego.
TypeScript
58
star
12

microscope

🔬 Microscope adds useful scopes targeting ActiveRecord boolean, date and datetime fields.
Ruby
55
star
13

emojimmy

[Deprecated] Emojimmy makes it possible to store emoji characters in ActiveRecord datastores that don’t support 4-Byte UTF-8 Unicode (utf8mb4) encoding.
Ruby
49
star
14

ekiden

GitHub Actions self-hosted arm64 macOS runners
HCL
46
star
15

killswitch

🪆 Killswitch is a clever control panel that allows mobile developers to apply runtime version-specific behaviors to their iOS or Android application.
Ruby
43
star
16

react-native-boilerplate

The stable base upon which we build our React Native projects at Mirego
TypeScript
39
star
17

ember-boilerplate

👓 The stable base upon which we build our Ember.js projects at Mirego.
TypeScript
36
star
18

trikot.streams

Reactive Streams for Kotlin Multiplatform with mutability checks
Kotlin
33
star
19

partisan

🚩 Partisan is a Ruby library that allows ActiveRecord records to be followers and followables.
Ruby
33
star
20

kmp-boilerplate

🧱 The stable base upon which we build our Kotlin Multiplatform projects at Mirego.
Kotlin
28
star
21

MCUIImageAdvanced

Advanced and powerful functionality enhancements to UIImage
Objective-C
27
star
22

activerecord_sane_schema_dumper

📝 ActiveRecord::SaneSchemaDumper removes useless/harmful whitespace from Rails’ generated `db/schema.rb` file.
Ruby
26
star
23

dispatch

🦀 Dispatch makes sure pull requests within a GitHub organization get reviewed by the right people.
Elixir
25
star
24

foreigner.js

Foreigner is a JavaScript library that will help you with translations.
JavaScript
25
star
25

bourgeois

🎩 Bourgeois is a Ruby library that makes using presenters a very simple thing.
Ruby
24
star
26

trikot.viewmodels

Meta abstraction of visual components for Kotlin Multiplatform
Kotlin
23
star
27

MCUIColorUtils

UIColor missing methods
Objective-C
22
star
28

MCAWSS3Client

Amazon S3 client based on AFHTTPClient
Objective-C
22
star
29

MCDateExtensions

Extensions on the dates classes
Objective-C
21
star
30

phare

🚨 Phare looks into your files and check for coding style errors.
Ruby
21
star
31

hanzo

🔪 Hanzo is a sharp tool to handle deploying an application on Heroku on multiple environments.
Ruby
20
star
32

trikot.foundation

Core utilities for Kotlin Multiplatform
Kotlin
20
star
33

simple-css-reset

🎏 A simple, no-nonsense CSS reset stylesheet to use as an NPM dependency.
CSS
20
star
34

ember-best-language

🏳 A FastBoot-enabled addon to detect the best language for your user.
TypeScript
19
star
35

active_model_serializers_validator

🃏 An extension to ActiveModel::Serializer that validates serializers output against a JSON schema
Ruby
18
star
36

illusionist

[Deprecated] Illusionist will make you believe ES6 is already available in browsers
JavaScript
18
star
37

MCAnimationView

UIImageView alternative for animations that don't need to load all the images in memory at once and provide callbacks when the animation is done.
Objective-C
16
star
38

trikot.http

HTTP networking abstraction for Kotlin Multiplatform
Kotlin
15
star
39

camaraderie

👬 Camaraderie takes away the pain of managing membership stuff between users and organizations.
Ruby
15
star
40

emotions

🎭 Allow ActiveRecord records to express (and hopefully store) emotions (eg. `happy`, `sadness`, `surprise`, etc.) about other records.
Ruby
14
star
41

absinthe_security

AbsintheSecurity provides utilities to improve the security posture of APIs built with Absinthe GraphQL.
Elixir
14
star
42

trikot.patron

Kotlin Multiplatform Sample Project using Trikot libraries
Kotlin
13
star
43

MCColoredPageControl

Fully customizable alternative to UIPageControl
Objective-C
11
star
44

plug_image_processing

Endpoint to process images within your Plug application, compatible with imaginary API
Elixir
11
star
45

taylor-ios

iOS Framework with a bunch of classes and helpers for Swift.
Swift
11
star
46

sprockets-illusionist

[Deprecated] Transpile your ES6 files into ES5 using the Illusionist node module and Sprockets
Ruby
11
star
47

rack-locale-root-redirect

🎌 Rack::LocaleRootRedirect uses Rack:Accept to map '/' to a path based on the Accept-Language HTTP header.
Ruby
11
star
48

parole

💬 Parole adds the ability to comment on ActiveRecord records
Ruby
10
star
49

louche

📋 Louche provides common validators for ActiveModel/ActiveRecord classes
Ruby
9
star
50

kotlin-ssml-dsl

A kotlin DSL for SSML, supports Actions-on-google.
Kotlin
9
star
51

trikot.kword

Localization library for Kotlin Multiplatform. Tailor-made for Accent.
Kotlin
9
star
52

encore

[Deprecated] Encore provides serializers and persisters to build JSON API-compliant Web services with Ruby on Rails.
Ruby
9
star
53

MCDebuggingTools

A collection of cool debugging tools
Objective-C
9
star
54

html_test_helpers

HTMLTestHelpers provides function helpers for unit testing allowing easy assertions for HTML elements data queried by data-testid attribute.
Elixir
8
star
55

accent-cli

Sync, merge and export with the Accent API.
TypeScript
8
star
56

halfday

⛳ Collection of Capistrano recipes — Deploy. Then, take a halfday.
Ruby
7
star
57

konnectivity

📡 A lightweight Kotlin Multiplatform library to monitor network state changes
Kotlin
7
star
58

mirego-open-web

Mirego ❤️ Open source (libraries & tools for everyone to use!)
JavaScript
6
star
59

html_test_identifiers

HTMLTestIdentifiers provides the basic functionality to add data-testid attribute depending on configuration.
Elixir
5
star
60

ember-cli-graphql-file

An addon to precompile your `.graphql` files with `graphql-tag` and turn them into importable modules.
JavaScript
5
star
61

csgames17-competition

CS Games 2017 Mobile Competition http://2017.csgames.org
Swift
5
star
62

trikot.analytics

Elegant implementation of multiplatform analytics in ReactiveStreams.
Kotlin
5
star
63

activerecord_strict_validations

🚧 ActiveRecord::StrictValidations adds validations to ActiveRecord models to make sure they don’t trigger database errors.
Ruby
5
star
64

viewmodel-pilot

Viewmodel pilot is a library that allows you to share viewmodels and navigation on android and iOS
Kotlin
5
star
65

MCHTTPRequestLogger

Output HTTP requests made with AFNetworking in the debug console
Objective-C
4
star
66

csgames16-competition

CS Games 2016 Mobile Competition
Swift
4
star
67

react-native-killswitch

The official React Native implementation of Mirego’s killswitch
TypeScript
4
star
68

ember-cli-foreigner

ember-cli-foreigner is a set of helpers and mixins to interface with the foreigner.js library in Ember.
JavaScript
4
star
69

csgames-sample-ios

CS Games - Sample iOS App
3
star
70

ember-chai-dom-helpers

A set of Chai helpers ready for your DOM.
JavaScript
3
star
71

csgames-sample-mobile

CS Games - Sample Mobile App
Swift
3
star
72

stylelint-mirego

💅 A collection of Stylelint rules that help our code meet our code styling guidelines.
JavaScript
3
star
73

compose-utils

Compose Utils is a library that contains several helpful components and extension methods to help you build the best Compose layouts.
Kotlin
3
star
74

ember-cli-pod-translations

An addon to compile your locale files inside pods.
JavaScript
2
star
75

trikot.datasources

Datasource and cache layers for Kotlin Multiplatform
Kotlin
2
star
76

csgames19-competition

CS Games 2019 Mobile Competition http://2019.csgames.org
Swift
2
star
77

MCForms-iOS

Forms engine for iOS in Swift
Swift
2
star
78

fastlane-toolkit

Boilerplate for faster fastlane integration
Ruby
2
star
79

csgames-sample-android

CS Games - Sample Android App
2
star
80

MRGLaunchImageViewController

A view controller that displays the launch image to ease your transitions at app launch
Objective-C
2
star
81

MCKillSwitch

MCKillSwitch is the official kill switch to use with Mirego’s backend implementation
Objective-C
2
star
82

swiftui-utils

Swift
2
star
83

debug-panel

A Kotlin Multiplatform library that allows mobile developers to generate boilerplate code to display a debug panel with different component types.
Kotlin
2
star
84

MRGPagerController

An highly customizable pager controller
Objective-C
1
star
85

csgames18-competition

CS Games 2018 Mobile Competition http://2018.csgames.org
Java
1
star
86

MRGPDFKit

A simple objective-C Toolkit to fill PDF forms.
Objective-C
1
star
87

csgames18-precompetition

CS Games 2018 Mobile Pre-competition
Swift
1
star
88

ember-cli-encore

[Deprecated] ember-cli-encore is an adapter and a serializer to use with the Encore gem.
JavaScript
1
star
89

MRGDiagnostics

An iOS framework that offers you an easy way to add a diagnostics view to your iOS project.
Swift
1
star
90

carlito

A Chrome extension to switch from Core to iOS / Android repositories
JavaScript
1
star
91

MRGControlPanel

The Control panel of your dream.
Objective-C
1
star
92

ember-encore

[Deprecated] EmberEncore is an adapter and a serializer to use with the Encore gem.
JavaScript
1
star
93

MRGArchitect

Provides your app with device-agnostic measurements.
Objective-C
1
star
94

heroku-buildpack-bower

Install Bower and Bower components
1
star
95

minimal-json

[Fork] Optimized version of minimal-json, a fast and small JSON parser and writer for Java.
Java
1
star
96

MRGLocale

Easily manage your localizations by adding dynamic (remote) refs to be able to update them without an app update
Objective-C
1
star
97

killswitch-mobile

Multiplatform library for using the Mirego Killswitch inside mobile applications
Kotlin
1
star
98

patch-patch

[Deprecated] PatchPatch changes Rails’ default behavior of mapping PUT and PATCH requests on resources to the same action.
Ruby
1
star