Skip to content

Commit a9e8cf7

Browse files
committed
add ActionController::Metal#show_detailed_exceptions?
1 parent 8f57bf2 commit a9e8cf7

File tree

4 files changed

+39
-29
lines changed

4 files changed

+39
-29
lines changed

actionpack/lib/action_controller/metal.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,15 @@ def dispatch(name, request) #:nodoc:
196196
@_request = request
197197
@_env = request.env
198198
@_env['action_controller.instance'] = self
199+
@_env['action_dispatch.show_detailed_exceptions'] = show_detailed_exceptions?
199200
process(name)
200201
to_a
201202
end
202203

204+
def show_detailed_exceptions?
205+
defined?(Rails.application) && Rails.application.config.consider_all_requests_local || request.local?
206+
end
207+
203208
def to_a #:nodoc:
204209
response ? response.to_a : [status, headers, response_body]
205210
end

actionpack/lib/action_dispatch/middleware/show_exceptions.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ class ShowExceptions
3838
"application's log file and/or the web server's log file to find out what " <<
3939
"went wrong.</body></html>"]]
4040

41-
def initialize(app, consider_all_requests_local = false)
41+
def initialize(app)
4242
@app = app
43-
@consider_all_requests_local = consider_all_requests_local
4443
end
4544

4645
def call(env)
@@ -65,11 +64,10 @@ def render_exception(env, exception)
6564
log_error(exception)
6665
exception = original_exception(exception)
6766

68-
request = Request.new(env)
69-
if @consider_all_requests_local || request.local?
70-
rescue_action_locally(request, exception)
67+
if env['action_dispatch.show_detailed_exceptions'] == true
68+
rescue_action_diagnostics(env, exception)
7169
else
72-
rescue_action_in_public(exception)
70+
rescue_action_error_page(exception)
7371
end
7472
rescue Exception => failsafe_error
7573
$stderr.puts "Error during failsafe response: #{failsafe_error}\n #{failsafe_error.backtrace * "\n "}"
@@ -78,9 +76,9 @@ def render_exception(env, exception)
7876

7977
# Render detailed diagnostics for unhandled exceptions rescued from
8078
# a controller action.
81-
def rescue_action_locally(request, exception)
79+
def rescue_action_diagnostics(env, exception)
8280
template = ActionView::Base.new([RESCUES_TEMPLATE_PATH],
83-
:request => request,
81+
:request => Request.new(env),
8482
:exception => exception,
8583
:application_trace => application_trace(exception),
8684
:framework_trace => framework_trace(exception),
@@ -98,7 +96,7 @@ def rescue_action_locally(request, exception)
9896
# it will first attempt to render the file at <tt>public/500.da.html</tt>
9997
# then attempt to render <tt>public/500.html</tt>. If none of them exist,
10098
# the body of the response will be left empty.
101-
def rescue_action_in_public(exception)
99+
def rescue_action_error_page(exception)
102100
status = status_code(exception)
103101
locale_path = "#{public_path}/#{status}.#{I18n.locale}.html" if I18n.locale
104102
path = "#{public_path}/#{status}.html"

actionpack/test/dispatch/show_exceptions_test.rb

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,35 @@
22

33
class ShowExceptionsTest < ActionDispatch::IntegrationTest
44

5-
Boomer = lambda do |env|
6-
req = ActionDispatch::Request.new(env)
7-
case req.path
8-
when "/not_found"
9-
raise ActionController::UnknownAction
10-
when "/runtime_error"
11-
raise RuntimeError
12-
when "/method_not_allowed"
13-
raise ActionController::MethodNotAllowed
14-
when "/not_implemented"
15-
raise ActionController::NotImplemented
16-
when "/unprocessable_entity"
17-
raise ActionController::InvalidAuthenticityToken
18-
when "/not_found_original_exception"
19-
raise ActionView::Template::Error.new('template', {}, AbstractController::ActionNotFound.new)
20-
else
21-
raise "puke!"
5+
class Boomer
6+
def initialize(show_exceptions = false)
7+
@show_exceptions = show_exceptions
8+
end
9+
10+
def call(env)
11+
env['action_dispatch.show_exceptions'] = @show_exceptions
12+
req = ActionDispatch::Request.new(env)
13+
case req.path
14+
when "/not_found"
15+
raise ActionController::UnknownAction
16+
when "/runtime_error"
17+
raise RuntimeError
18+
when "/method_not_allowed"
19+
raise ActionController::MethodNotAllowed
20+
when "/not_implemented"
21+
raise ActionController::NotImplemented
22+
when "/unprocessable_entity"
23+
raise ActionController::InvalidAuthenticityToken
24+
when "/not_found_original_exception"
25+
raise ActionView::Template::Error.new('template', {}, AbstractController::ActionNotFound.new)
26+
else
27+
raise "puke!"
28+
end
2229
end
2330
end
2431

25-
ProductionApp = ActionDispatch::ShowExceptions.new(Boomer, false)
26-
DevelopmentApp = ActionDispatch::ShowExceptions.new(Boomer, true)
32+
ProductionApp = ActionDispatch::ShowExceptions.new(Boomer.new(false))
33+
DevelopmentApp = ActionDispatch::ShowExceptions.new(Boomer.new(true))
2734

2835
test "rescue in public from a remote ip" do
2936
@app = ProductionApp

railties/lib/rails/application.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def default_middleware_stack
166166
middleware.use ::Rack::MethodOverride
167167
middleware.use ::ActionDispatch::RequestId
168168
middleware.use ::Rails::Rack::Logger, config.log_tags # must come after Rack::MethodOverride to properly log overridden methods
169-
middleware.use ::ActionDispatch::ShowExceptions, config.consider_all_requests_local
169+
middleware.use ::ActionDispatch::ShowExceptions
170170
middleware.use ::ActionDispatch::RemoteIp, config.action_dispatch.ip_spoofing_check, config.action_dispatch.trusted_proxies
171171
if config.action_dispatch.x_sendfile_header.present?
172172
middleware.use ::Rack::Sendfile, config.action_dispatch.x_sendfile_header

0 commit comments

Comments
 (0)