Skip to content

Instantly share code, notes, and snippets.

@ajh
Forked from s-andringa/Locales.yml
Created February 13, 2014 14:26
Show Gist options
  • Select an option

  • Save ajh/8975881 to your computer and use it in GitHub Desktop.

Select an option

Save ajh/8975881 to your computer and use it in GitHub Desktop.

Revisions

  1. Sjoerd Andringa revised this gist Sep 19, 2012. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions Ruby.rb
    Original file line number Diff line number Diff line change
    @@ -40,5 +40,11 @@ def details

    # app/controllers/articles_controller.rb

    class ::BadTaste < ::StandardError; end
    raise BadTaste, "Clams are grose" if @article.title =~ /vongole/i
    class ArticlesController < ApplicationController
    def show
    # ...
    raise BadTaste, "Clams are grose" if @article.title =~ /vongole/i
    end
    # ...
    class ::BadTaste < ::StandardError; end
    end
  2. Sjoerd Andringa created this gist Sep 19, 2012.
    17 changes: 17 additions & 0 deletions Locales.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    # config/locales/en.yml

    en:
    exception:
    show:
    not_found:
    title: "Not Found"
    description: "The page you were looking for does not exists."
    internal_server_error:
    title: "Internal Server Error"
    description: "Sorry, something went wrong while processing your request."
    bad_request:
    title: "Bad Request"
    description: "The server did not understand your request. It may have been malformed."
    bad_taste:
    title: "Bad Taste"
    description: "Sorry, the server thinks you've got a bad taste for food (%{exception_message})."
    44 changes: 44 additions & 0 deletions Ruby.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    # config/environments/development.rb

    config.consider_all_requests_local = false # true

    # config/application.rb

    config.exceptions_app = ->(env) { ExceptionController.action(:show).call(env) }
    config.action_dispatch.rescue_responses["BadTaste"] = :bad_request

    # app/controllers/exception_controller.rb

    class ExceptionController < ActionController::Base
    layout 'application'

    def show
    @exception = env['action_dispatch.exception']
    @status_code = ActionDispatch::ExceptionWrapper.new(env, @exception).status_code
    @rescue_response = ActionDispatch::ExceptionWrapper.rescue_responses[@exception.class.name]

    respond_to do |format|
    format.html { render :show, status: @status_code, layout: !request.xhr? }
    format.xml { render xml: details, root: "error", status: @status_code }
    format.json { render json: {error: details}, status: @status_code }
    end
    end

    protected

    def details
    @details ||= {}.tap do |h|
    I18n.with_options scope: [:exception, :show, @rescue_response], exception_name: @exception.class.name, exception_message: @exception.message do |i18n|
    h[:name] = i18n.t "#{@exception.class.name.underscore}.title", default: i18n.t(:title, default: @exception.class.name)
    h[:message] = i18n.t "#{@exception.class.name.underscore}.description", default: i18n.t(:description, default: @exception.message)
    end
    end
    end
    helper_method :details

    end

    # app/controllers/articles_controller.rb

    class ::BadTaste < ::StandardError; end
    raise BadTaste, "Clams are grose" if @article.title =~ /vongole/i
    7 changes: 7 additions & 0 deletions Templates.html.erb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    <!-- app/views/exception/show.htm.erb -->

    <div class="box">
    <h1><%= details[:name] %></h1>
    <p><%= details[:message] %></p>
    <%= link_to "Back to home", root_path %>
    </div>