-
-
Save ajh/8975881 to your computer and use it in GitHub Desktop.
Revisions
-
Sjoerd Andringa revised this gist
Sep 19, 2012 . 1 changed file with 8 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -40,5 +40,11 @@ def details # app/controllers/articles_controller.rb class ArticlesController < ApplicationController def show # ... raise BadTaste, "Clams are grose" if @article.title =~ /vongole/i end # ... class ::BadTaste < ::StandardError; end end -
Sjoerd Andringa created this gist
Sep 19, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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})." This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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>