1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add source extract to detailed exception page

This commit is contained in:
Guillermo Iguaran 2012-04-29 18:26:37 -05:00
parent 90c8972516
commit fe12e46508
4 changed files with 34 additions and 2 deletions

View file

@ -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}"

View file

@ -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

View file

@ -0,0 +1,8 @@
<% if @source_extract %>
Extracted source (around line <strong>#<%= @line_number %></strong>):
<pre>
<% @source_extract.each do |line, source| %>
<%= "#{(@line_number == line) ? "> " : " "}#{line}: #{source}" -%>
<% end %>
</pre>
<% end %>

View file

@ -6,5 +6,6 @@
</h1>
<pre><%=h @exception.message %></pre>
<%= render template: "rescues/_source" %>
<%= render template: "rescues/_trace" %>
<%= render template: "rescues/_request_and_response" %>