Skip to content

Commit 686a6de

Browse files
Alexey PalazhchenkoLennart Koopmann
Alexey Palazhchenko
authored and
Lennart Koopmann
committed
Clean-up exception handling.
1 parent 93568f5 commit 686a6de

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

app/controllers/application_controller.rb

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,42 @@
33

44
class ApplicationController < ActionController::Base
55
include AuthenticatedSystem
6-
6+
77
helper :all # include all helpers, all the time
88
protect_from_forgery # See ActionController::RequestForgeryProtection for details
9-
9+
1010
# Scrub sensitive parameters from your log
1111
filter_parameter_logging :password
1212

1313
before_filter :login_required
1414

15-
def rescue_action e
16-
# Connection to MongoDB failed.
17-
if e.class == Mongo::ConnectionFailure
18-
render :file => "#{RAILS_ROOT}/public/mongo_connectionfailure.html", :status => 500
19-
return
15+
rescue_from Exception do |e|
16+
if consider_all_requests_local || local_request?
17+
rescue_action_without_handler e
18+
else
19+
logger.error "ERROR: #{e.to_s}"
20+
render :file => "#{RAILS_ROOT}/public/500.html", :status => 500
2021
end
22+
end
2123

22-
# Default 404 error.
23-
if e.class == ActionController::RoutingError
24-
render :file => "#{RAILS_ROOT}/public/404.html", :status => 404
25-
return
24+
rescue_from ActionView::TemplateError do |e|
25+
# This rescue_from handling all kind of errors which is raised during template rendering.
26+
# So, if environment is development, we should handle it as usual.
27+
# If environment is test or production, we should handle it in particular way –
28+
# just reraise the exception so that other rescue_from can handle it.
29+
if Rails.env.development?
30+
rescue_action_without_handler e
31+
else
32+
rescue_with_handler e.original_exception
2633
end
34+
end
35+
36+
rescue_from Mongo::ConnectionFailure do |e|
37+
render :file => "#{RAILS_ROOT}/public/mongo_connectionfailure.html", :status => 500
38+
end
2739

28-
# Default 500 error.
29-
logger.error "ERROR: #{e.to_s}"
30-
render :file => "#{RAILS_ROOT}/public/500.html", :status => 500
40+
rescue_from ActionController::RoutingError do |e|
41+
render :file => "#{RAILS_ROOT}/public/404.html", :status => 404
3142
end
3243

3344
helper_method :has_users
@@ -44,7 +55,7 @@ def logged_in?
4455
end
4556
return false
4657
end
47-
58+
4859
def login_required
4960
if !logged_in?
5061
redirect_to :controller => "sessions", :action => "new"

test/functional/sessions_controller_test.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
require File.dirname(__FILE__) + '/../test_helper'
22
require 'sessions_controller'
33

4-
# Re-raise errors caught by the controller.
5-
class SessionsController; def rescue_action(e) raise e end; end
6-
74
class SessionsControllerTest < ActionController::TestCase
85
# Be sure to include AuthenticatedTestHelper in test/test_helper.rb instead
96
# Then, you can remove it from this and the units test.

test/functional/users_controller_test.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
require File.dirname(__FILE__) + '/../test_helper'
22
require 'users_controller'
33

4-
# Re-raise errors caught by the controller.
5-
class UsersController; def rescue_action(e) raise e end; end
6-
74
class UsersControllerTest < ActionController::TestCase
85
# Be sure to include AuthenticatedTestHelper in test/test_helper.rb instead
96
# Then, you can remove it from this and the units test.

0 commit comments

Comments
 (0)