Skip to content

Commit

Permalink
Use the Rails 6 Interceptor API and avoid a monkey patch
Browse files Browse the repository at this point in the history
  • Loading branch information
gsamokovarov committed Apr 25, 2019
1 parent b249558 commit d6deacd
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 59 deletions.
1 change: 1 addition & 0 deletions lib/web_console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module WebConsole
autoload :ExceptionMapper
autoload :Session
autoload :Injector
autoload :Interceptor
autoload :Request
autoload :WhinyRequest
autoload :Permissions
Expand Down
24 changes: 0 additions & 24 deletions lib/web_console/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,3 @@ def console(binding = Bindex.current_bindings.second)
nil
end
end

module ActionDispatch
class DebugExceptions
def render_exception_with_web_console(request, exception)
render_exception_without_web_console(request, exception).tap do
backtrace_cleaner = request.get_header("action_dispatch.backtrace_cleaner")
error = ExceptionWrapper.new(backtrace_cleaner, exception).exception

# Get the original exception if ExceptionWrapper decides to follow it.
Thread.current[:__web_console_exception] = error

# ActionView::Template::Error bypass ExceptionWrapper original
# exception following. The backtrace in the view is generated from
# reaching out to original_exception in the view.
if error.is_a?(ActionView::Template::Error)
Thread.current[:__web_console_exception] = error.cause
end
end
end

alias_method :render_exception_without_web_console, :render_exception
alias_method :render_exception, :render_exception_with_web_console
end
end
18 changes: 18 additions & 0 deletions lib/web_console/interceptor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module WebConsole
module Interceptor
def self.call(request, exception)
backtrace_cleaner = request.get_header("action_dispatch.backtrace_cleaner")
error = ActionDispatch::ExceptionWrapper.new(backtrace_cleaner, exception).exception

# Get the original exception if ExceptionWrapper decides to follow it.
Thread.current[:__web_console_exception] = error

# ActionView::Template::Error bypass ExceptionWrapper original
# exception following. The backtrace in the view is generated from
# reaching out to original_exception in the view.
if error.is_a?(ActionView::Template::Error)
Thread.current[:__web_console_exception] = error.cause
end
end
end
end
2 changes: 2 additions & 0 deletions lib/web_console/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Railtie < ::Rails::Railtie
initializer "web_console.initialize" do
require "bindex"
require "web_console/extensions"

ActionDispatch::DebugExceptions.register_interceptor(Interceptor)
end

initializer "web_console.development_only" do
Expand Down
34 changes: 0 additions & 34 deletions test/web_console/extensions_test.rb

This file was deleted.

26 changes: 26 additions & 0 deletions test/web_console/interceptor_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require "test_helper"

module WebConsole
class InterceptorTest < ActionDispatch::IntegrationTest
test "follows ActionView::Template::Error original error in Thread.current[:__web_console_exception]" do
request = Request.new({})
request.set_header("action_dispatch.backtrace_cleaner", ActiveSupport::BacktraceCleaner.new)

Interceptor.call(request, generate_template_error)

assert_equal 42, Thread.current[:__web_console_exception].bindings.first.eval("@ivar")
end

def generate_template_error
WebConsole::View.new(ActionView::LookupContext.new([])).render(inline: <<~ERB)
<% @ivar = 42 %>
<%= nil.raise %>
</h1
ERB
rescue ActionView::Template::Error => err
err
end
end
end
2 changes: 1 addition & 1 deletion web-console.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Gem::Specification.new do |s|

s.required_ruby_version = ">= 2.4"

rails_version = ">= 5.2"
rails_version = ">= 6.0.0.a"

s.add_dependency "railties", rails_version
s.add_dependency "activemodel", rails_version
Expand Down

0 comments on commit d6deacd

Please sign in to comment.