From fe12e4650802d8f28136660fc9ce62c6a28f19e1 Mon Sep 17 00:00:00 2001 From: Guillermo Iguaran Date: Sun, 29 Apr 2012 18:26:37 -0500 Subject: Add source extract to detailed exception page --- .../action_dispatch/middleware/debug_exceptions.rb | 5 ++++- .../middleware/exception_wrapper.rb | 22 +++++++++++++++++++++- .../middleware/templates/rescues/_source.erb | 8 ++++++++ .../middleware/templates/rescues/diagnostics.erb | 1 + 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 actionpack/lib/action_dispatch/middleware/templates/rescues/_source.erb (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb b/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb index 6705e531cb..ac8f6af7fe 100644 --- a/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb +++ b/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb @@ -42,7 +42,10 @@ module ActionDispatch :application_trace => wrapper.application_trace, :framework_trace => wrapper.framework_trace, :full_trace => wrapper.full_trace, - :routes => formatted_routes(exception) + :routes => formatted_routes(exception), + :source_extract => wrapper.source_extract, + :line_number => wrapper.line_number, + :file => wrapper.file ) file = "rescues/#{wrapper.rescue_template}" 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 diff --git a/actionpack/lib/action_dispatch/middleware/templates/rescues/_source.erb b/actionpack/lib/action_dispatch/middleware/templates/rescues/_source.erb new file mode 100644 index 0000000000..032705c01a --- /dev/null +++ b/actionpack/lib/action_dispatch/middleware/templates/rescues/_source.erb @@ -0,0 +1,8 @@ +<% if @source_extract %> +Extracted source (around line #<%= @line_number %>): +
+<% @source_extract.each do |line, source| %>
+<%= "#{(@line_number == line) ? "> " : "  "}#{line}: #{source}" -%>
+<% end %>
+
+<% end %> diff --git a/actionpack/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb b/actionpack/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb index c5043c5e7b..5ad96cc657 100644 --- a/actionpack/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb +++ b/actionpack/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb @@ -6,5 +6,6 @@
<%=h @exception.message %>
+<%= render template: "rescues/_source" %> <%= render template: "rescues/_trace" %> <%= render template: "rescues/_request_and_response" %> -- cgit v1.2.3