diff options
author | Guillermo Iguaran <guilleiguaran@gmail.com> | 2012-04-29 18:26:37 -0500 |
---|---|---|
committer | Guillermo Iguaran <guilleiguaran@gmail.com> | 2012-12-31 13:48:00 -0500 |
commit | fe12e4650802d8f28136660fc9ce62c6a28f19e1 (patch) | |
tree | 686da31d67a7189e017d87175ad07e5c2fe6a29b /actionpack/lib/action_dispatch/middleware/exception_wrapper.rb | |
parent | 90c8972516d683f73dd2354f4b44e42d73c71a29 (diff) | |
download | rails-fe12e4650802d8f28136660fc9ce62c6a28f19e1.tar.gz rails-fe12e4650802d8f28136660fc9ce62c6a28f19e1.tar.bz2 rails-fe12e4650802d8f28136660fc9ce62c6a28f19e1.zip |
Add source extract to detailed exception page
Diffstat (limited to 'actionpack/lib/action_dispatch/middleware/exception_wrapper.rb')
-rw-r--r-- | actionpack/lib/action_dispatch/middleware/exception_wrapper.rb | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/actionpack/lib/action_dispatch/middleware/exception_wrapper.rb b/actionpack/lib/action_dispatch/middleware/exception_wrapper.rb index ae38c56a67..d1cadbd082 100644 --- a/actionpack/lib/action_dispatch/middleware/exception_wrapper.rb +++ b/actionpack/lib/action_dispatch/middleware/exception_wrapper.rb @@ -25,7 +25,7 @@ module ActionDispatch 'ActionView::Template::Error' => 'template_error' ) - attr_reader :env, :exception + attr_reader :env, :exception, :line_number, :file def initialize(env, exception) @env = env @@ -56,6 +56,15 @@ module ActionDispatch Rack::Utils.status_code(@@rescue_responses[class_name]) end + def source_extract + if trace = application_trace.first + file, line, _ = trace.split(":") + @file = file + @line_number = line.to_i + source_fragment(@file, @line_number) + end + end + private def original_exception(exception) @@ -81,5 +90,16 @@ module ActionDispatch def backtrace_cleaner @backtrace_cleaner ||= @env['action_dispatch.backtrace_cleaner'] end + + def source_fragment(path, line) + full_path = Rails.root.join(path) + if File.exists?(full_path) + File.open(full_path, "r") do |file| + start = [line - 3, 0].max + lines = file.lines.drop(start).take(6) + Hash[*(start+1..(lines.count+start)).zip(lines).flatten] + end + end + end end end |