Skip to content

Commit a613d6b

Browse files
committed
Merge pull request #115 from gsamokovarov/follow-original-exception
Show proper binding when raising an error in a template
2 parents 4ad861c + 261265c commit a613d6b

File tree

6 files changed

+55
-12
lines changed

6 files changed

+55
-12
lines changed

lib/web_console/extensions.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
ActionDispatch::DebugExceptions.class_eval do
2+
def render_exception_with_web_console(env, exception)
3+
render_exception_without_web_console(env, exception).tap do
4+
error = ActionDispatch::ExceptionWrapper.new(env, exception).exception
5+
6+
# Get the original exception if ExceptionWrapper decides to follow it.
7+
env['web_console.exception'] = error
8+
9+
# ActionView::Template::Error bypass ExceptionWrapper original
10+
# exception following. The backtrace in the view is generated from
11+
# reaching out to original_exception in the view.
12+
if error.is_a?(ActionView::Template::Error)
13+
env['web_console.exception'] = error.original_exception
14+
end
15+
end
16+
end
17+
18+
alias_method_chain :render_exception, :web_console
19+
end

lib/web_console/railtie.rb

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,7 @@ class Railtie < ::Rails::Railtie
66
config.web_console.whitelisted_ips = %w( 127.0.0.1 ::1 )
77

88
initializer 'web_console.initialize' do
9-
ActionDispatch::DebugExceptions.class_eval do
10-
def render_exception_with_web_console(env, exception)
11-
render_exception_without_web_console(env, exception).tap do
12-
wrapper = ActionDispatch::ExceptionWrapper.new(env, exception)
13-
14-
# Get the original exception if ExceptionWrapper decides to follow it.
15-
env['web_console.exception'] = wrapper.exception
16-
end
17-
end
18-
19-
alias_method_chain :render_exception, :web_console
20-
end
9+
require 'web_console/extensions'
2110

2211
ActiveSupport.on_load(:action_view) do
2312
ActionView::Base.send(:include, Helper)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class HelperErrorController < ApplicationController
2+
def index
3+
end
4+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<% @ok = 42 %>
2+
<% raise %>

test/dummy/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
get :exception_test, to: "exception_test#index"
55
get :xhr_test, to: "exception_test#xhr"
66
get :helper_test, to: "helper_test#index"
7+
get :helper_error, to: "helper_error#index"
78
get :controller_helper_test, to: "controller_helper_test#index"
89

910
namespace :tests do
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'test_helper'
2+
require 'web_console/extensions'
3+
4+
module ActionDispatch
5+
class DebugExceptionsTest < ActionDispatch::IntegrationTest
6+
class Application
7+
def call(env)
8+
ActionView::Base.new.render(inline: '<% @ivar = 42 %> <%= nil.raise %></h1')
9+
end
10+
end
11+
12+
setup do
13+
Request.stubs(:whitelisted_ips).returns(IPAddr.new('0.0.0.0/0'))
14+
15+
@app = DebugExceptions.new(Application.new)
16+
end
17+
18+
test "follows ActionView::Template::Error original error in env['web_console.exception']" do
19+
get "/", {}, {
20+
'action_dispatch.show_detailed_exceptions' => true,
21+
'action_dispatch.show_exceptions' => true,
22+
'action_dispatch.logger' => Logger.new(StringIO.new)
23+
}
24+
25+
assert_equal 42, request.env['web_console.exception'].bindings.first.eval('@ivar')
26+
end
27+
end
28+
end

0 commit comments

Comments
 (0)