forked from gsamokovarov/web-console
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use the Rails 6 Interceptor API and avoid a monkey patch
- Loading branch information
1 parent
b249558
commit d6deacd
Showing
7 changed files
with
48 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters