{"id":13747350,"url":"https://github.com/mirego/gaffe","last_synced_at":"2025-05-09T08:32:26.683Z","repository":{"id":9485891,"uuid":"11374791","full_name":"mirego/gaffe","owner":"mirego","description":"💥 Gaffe handles Rails error pages in a clean, simple way.","archived":false,"fork":false,"pushed_at":"2018-05-23T18:15:58.000Z","size":70,"stargazers_count":249,"open_issues_count":6,"forks_count":19,"subscribers_count":53,"default_branch":"master","last_synced_at":"2025-04-16T10:07:36.157Z","etag":null,"topics":["404","rails"],"latest_commit_sha":null,"homepage":"http://open.mirego.com","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mirego.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-07-12T18:01:19.000Z","updated_at":"2025-02-13T12:52:38.000Z","dependencies_parsed_at":"2022-09-09T09:21:01.997Z","dependency_job_id":null,"html_url":"https://github.com/mirego/gaffe","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mirego%2Fgaffe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mirego%2Fgaffe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mirego%2Fgaffe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mirego%2Fgaffe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mirego","download_url":"https://codeload.github.com/mirego/gaffe/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253217136,"owners_count":21873018,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["404","rails"],"created_at":"2024-08-03T06:01:25.794Z","updated_at":"2025-05-09T08:32:26.400Z","avatar_url":"https://github.com/mirego.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://github.com/mirego/gaffe\"\u003e\n    \u003cimg src=\"http://i.imgur.com/k9Vo08q.png\" alt=\"gaffe\" /\u003e\n  \u003c/a\u003e\n  \u003cbr /\u003e\n  Gaffe makes having customized error pages in Rails applications an easy thing.\u003cbr /\u003e It takes advantage of a feature present in Rails 3.2 (and 4.0+, obviously) called \u003ccode\u003eexceptions_app\u003c/code\u003e.\n  \u003cbr /\u003e\u003cbr /\u003e\n  \u003ca href=\"https://rubygems.org/gems/gaffe\"\u003e\u003cimg src=\"http://img.shields.io/gem/v/gaffe.svg\" /\u003e\u003c/a\u003e\n  \u003ca href=\"https://codeclimate.com/github/mirego/gaffe\"\u003e\u003cimg src=\"http://img.shields.io/codeclimate/github/mirego/gaffe.svg\" /\u003e\u003c/a\u003e\n  \u003ca href='https://gemnasium.com/mirego/gaffe'\u003e\u003cimg src=\"http://img.shields.io/gemnasium/mirego/gaffe.svg\" /\u003e\u003c/a\u003e\n  \u003ca href=\"https://travis-ci.org/mirego/gaffe\"\u003e\u003cimg src=\"http://img.shields.io/travis/mirego/gaffe.svg\" /\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n---\n\nIt comes with default error pages but makes it very easy to override them (which you should do). The default error pages look like this:\n\n![](http://i.imgur.com/nz5nWXn.png)\n\n## Installation\n\nAdd this line to your application’s Gemfile:\n\n```ruby\ngem 'gaffe'\n```\n\n## Usage\n\nThe easiest way to use Gaffe is with an initializer:\n\n```ruby\n# config/initializers/gaffe.rb\nGaffe.enable!\n```\n\n### Custom controller\n\nHowever, if you want to use your own controller:\n\n```ruby\n# config/initializers/gaffe.rb\nGaffe.configure do |config|\n  config.errors_controller = 'ErrorsController'\nend\n\nGaffe.enable!\n```\n\nIt’s also possible to use a custom controller based on the URL in which the error has occured. Both absolute and \nrelative URL supported. This is especially useful if you have an application that also serves API requests via \nJSON. You would probably want to serve API errors through JSON and regular errors through HTML pages.\n\n```ruby\n# config/initializers/gaffe.rb\nGaffe.configure do |config|\n  config.errors_controller = {\n    %r[^/api/] =\u003e 'Api::ErrorsController',\n    %r[^/] =\u003e 'ErrorsController',\n    %r[^www.example.com] =\u003e 'HostSpecificErrorsController'\n  }\nend\n\nGaffe.enable!\n```\n\nThe only required thing to do in your custom controller is to include the `Gaffe::Errors` module.\n\nOnly `show` will be called so you might want to override it. If you don’t override it, Gaffe will\ntry to render the view `\"errors/#{@rescue_response}\"` within your application (or use its default\nerror page if the view doesn’t exist).\n\nYou might also want to get rid of filters and other stuff to make sure that error pages are always accessible.\n\n```ruby\nclass ErrorsController \u003c ApplicationController\n  include Gaffe::Errors\n\n  # Make sure anonymous users can see the page\n  skip_before_action :authenticate_user!\n\n  # Override 'error' layout\n  layout 'application'\n\n  # Render the correct template based on the exception “standard” code.\n  # Eg. For a 404 error, the `errors/not_found` template will be rendered.\n  def show\n    # Here, the `@exception` variable contains the original raised error\n    render \"errors/#{@rescue_response}\", status: @status_code\n  end\nend\n```\n\nFor example, you might want your `API::ErrorsController` to return a standard JSON response:\n\n```ruby\nclass API::ErrorsController \u003c API::ApplicationController\n  include Gaffe::Errors\n\n  # Make sure anonymous users can see the page\n  skip_before_action :authenticate_user!\n\n  # Disable layout (your `API::ApplicationController` probably does this already)\n  layout false\n\n  # Render a simple JSON response containing the error “standard” code\n  # plus the exception name and backtrace if we’re in development.\n  def show\n    output = { error: @rescue_response }\n    output.merge! exception: @exception.inspect, backtrace: @exception.backtrace.first(10) if Rails.env.development? || Rails.env.test?\n    render json: output, status: @status_code\n  end\nend\n```\n\n### Custom views\n\nYou can (and should!) also use your own views. You just have to create a layout:\n\n```erb\n\u003c!-- app/views/layouts/error.html.erb --\u003e\n\u003ch1\u003eError!\u003c/h1\u003e\n\u003c%= yield %\u003e\n```\n\nAnd create a different view for [each possible error rescue response](https://github.com/mirego/gaffe/tree/master/app/views/errors) ([rails reference](https://github.com/rails/rails/blob/f9ceefd3b9c3cea2460a89799156f2c532c4491c/actionpack/lib/action_dispatch/middleware/exception_wrapper.rb)). For example, for `404` errors:\n\n```erb\n\u003c!-- app/views/errors/not_found.html.erb --\u003e\n\u003cp\u003eThis page does not exist.\u003c/p\u003e\n```\n\n### Custom exceptions\n\nIf your application is raising custom exceptions (through gems or your code)\nand you want to render specific views when it happens, you can map them to\nspecific rescue responses.\n\n```ruby\n# config/application.rb\nconfig.action_dispatch.rescue_responses.merge! 'CanCan::AccessDenied' =\u003e :forbidden\nconfig.action_dispatch.rescue_responses.merge! 'MyCustomException' =\u003e :not_acceptable\n```\n\n### Rails development environment\n\nRails prefers to render its own debug-friendly errors in the `development` environment,\nwhich is totally understandable. However, if you want to test Gaffe’s behavior in development\nyou’ll have to edit the `config/environments/development.rb` file.\n\n```ruby\n# Make Rails use `exceptions_app` in development\nconfig.consider_all_requests_local = false\n```\n\n### Rails test environment\n\nYou also have to configure Rails’ `test` environment so it lets Gaffe handle exceptions\nin request tests. You’ll have to edit the `config/environments/test.rb` file.\n\n```ruby\n# Make Rails use `exceptions_app` in tests\nconfig.consider_all_requests_local = false\n\n# Render exceptions instead of raising them\nconfig.action_dispatch.show_exceptions = true\n```\n\nUnfortunately, controller tests (called *functional tests* in Rails) do not\nwork with Gaffe, since they only test method calls in the controller class —\nthey do not go through the entire Rack stack to simulate a real HTTP request.\n\nTo test responses sent by Gaffe, you must use *request tests*.\n\n## Contributors\n\n* [@remiprev](https://github.com/remiprev)\n* [@simonprev](https://github.com/simonprev)\n* [@jmuheim](https://github.com/jmuheim)\n\n## License\n\n`Gaffe` is © 2013-2016 [Mirego](http://www.mirego.com) and may be freely distributed under the [New BSD license](http://opensource.org/licenses/BSD-3-Clause).  See the [`LICENSE.md`](https://github.com/mirego/gaffe/blob/master/LICENSE.md) file.\n\nThe mushroom cloud logo is based on [this lovely icon](http://thenounproject.com/noun/mushroom-cloud/#icon-No18596) by [Gokce Ozan](http://thenounproject.com/occultsearcher), from The Noun Project. Used under a [Creative Commons BY 3.0](http://creativecommons.org/licenses/by/3.0/) license.\n\n## About Mirego\n\n[Mirego](http://mirego.com) 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](http://life.mirego.com) who imagine and build beautiful Web and mobile applications. We come together to share ideas and [change the world](http://mirego.org).\n\nWe also [love open-source software](http://open.mirego.com) and we try to give back to the community as much as we can.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmirego%2Fgaffe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmirego%2Fgaffe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmirego%2Fgaffe/lists"}