Skip to content

Commit

Permalink
Use Module#prepend when defined instead of alias_method
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatack committed Apr 2, 2015
1 parent 71ca0cb commit e66b69a
Showing 1 changed file with 37 additions and 13 deletions.
50 changes: 37 additions & 13 deletions lib/web_console/extensions.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
ActionDispatch::DebugExceptions.class_eval do
def render_exception_with_web_console(env, exception)
render_exception_without_web_console(env, exception).tap do
error = ActionDispatch::ExceptionWrapper.new(env, exception).exception
if defined?(Module.prepend) && Rails.version >= '4'

# Get the original exception if ExceptionWrapper decides to follow it.
env['web_console.exception'] = error
module RenderExceptionsWithWebConsole
def render_exception(env, exception)
super.tap do
error = ActionDispatch::ExceptionWrapper.new(env, exception).exception

# 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)
env['web_console.exception'] = error.original_exception
# Get the original exception if ExceptionWrapper decides to follow it.
env['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)
env['web_console.exception'] = error.original_exception
end
end
end
end

module ActionDispatch
class DebugExceptions
prepend RenderExceptionsWithWebConsole
end
end

else

# Legacy code that duplicates the module above, using `alias_method_chain`.
# TODO: Remove me when support for Ruby < 2 && Rails < 4 is dropped.
ActionDispatch::DebugExceptions.class_eval do
def render_exception_with_web_console(env, exception)
render_exception_without_web_console(env, exception).tap do
error = ActionDispatch::ExceptionWrapper.new(env, exception).exception
env['web_console.exception'] = error
if error.is_a?(ActionView::Template::Error)
env['web_console.exception'] = error.original_exception
end
end
end
alias_method_chain :render_exception, :web_console
end

alias_method :render_exception_without_web_console, :render_exception
alias_method :render_exception, :render_exception_with_web_console
end

0 comments on commit e66b69a

Please sign in to comment.