diff options
author | Louise Crow <louise.crow@gmail.com> | 2013-05-02 10:23:26 +0100 |
---|---|---|
committer | Louise Crow <louise.crow@gmail.com> | 2013-05-02 13:28:31 +0100 |
commit | 6acce073443fbd700f346b1bf99ee72be3e4f387 (patch) | |
tree | 2d6c4277e7f11103217b4749cf8dfbca6b5eaba1 | |
parent | 0d9045ca1c6b2e2c2889e9237ed96ad689eec902 (diff) |
Clearer setting of status code, addition of notification.
-rw-r--r-- | app/controllers/application_controller.rb | 27 | ||||
-rw-r--r-- | spec/integration/errors_spec.rb | 8 |
2 files changed, 22 insertions, 13 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 7c9585955..b8bdc403c 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -13,9 +13,7 @@ class ApplicationController < ActionController::Base class PermissionDenied < StandardError end # assign our own handler method for non-local exceptions - if ! Rails.application.config.consider_all_requests_local - rescue_from Exception, :with => :render_exception - end + rescue_from Exception, :with => :render_exception # Standard headers, footers and navigation for whole site layout "default" @@ -117,19 +115,28 @@ class ApplicationController < ActionController::Base end def render_exception(exception) + + # In development, or the admin interface, or for a local request, let Rails handle the exception + # with its stack trace templates. Local requests in testing are a special case so that we can + # test this method - there we use consider_all_requests_local to control behaviour. + if Rails.application.config.consider_all_requests_local || local_request? || + (request.local? && !Rails.env.test?) + raise exception + end + @exception_backtrace = exception.backtrace.join("\n") @exception_class = exception.class.to_s @exception_message = exception.message - status_code = case exception - when ActiveRecord::RecordNotFound, - ActionController::UnknownAction - 404 + case exception + when ActiveRecord::RecordNotFound + @status = 404 when PermissionDenied - 403 + @status = 403 else - 500 + ExceptionNotifier::Notifier.exception_notification(request.env, exception).deliver + @status = 500 end - render :template => "general/exception_caught", :status => status_code + render :template => "general/exception_caught", :status => @status end # Override default error handler, for production sites. diff --git a/spec/integration/errors_spec.rb b/spec/integration/errors_spec.rb index 6069a69b8..ccf3c4379 100644 --- a/spec/integration/errors_spec.rb +++ b/spec/integration/errors_spec.rb @@ -3,15 +3,12 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe "When errors occur" do def set_consider_all_requests_local(value) - # Reload application controller so it picks up new config value @requests_local = Rails.application.config.consider_all_requests_local Rails.application.config.consider_all_requests_local = value - load 'application_controller.rb' end def restore_consider_all_requests_local Rails.application.config.consider_all_requests_local = @requests_local - load "application_controller.rb" end before(:each) do @@ -56,6 +53,7 @@ describe "When errors occur" do end end + it "should render a 500 for general errors using the general/exception_caught template" do InfoRequest.stub!(:find_by_url_title!).and_raise("An example error") get("/request/example") @@ -94,12 +92,16 @@ describe "When errors occur" do end context "in the admin interface" do + it 'should show a full trace for general errors' do InfoRequest.stub!(:find).and_raise("An example error") get("/admin/request/show/333") response.body.should have_selector('div[id=traces]') response.body.should match('An example error') end + end + end + end |