Skip to content

Commit

Permalink
Merge pull request #17636 from gsamokovarov/exception-wrapper-less-nils
Browse files Browse the repository at this point in the history
Don't let #{application,framework,full}_trace be nil
  • Loading branch information
guilleiguaran committed Nov 16, 2014
2 parents 4b4dca4 + e05714f commit 7ae7ae5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 31 deletions.
24 changes: 13 additions & 11 deletions actionpack/lib/action_dispatch/middleware/exception_wrapper.rb
Expand Up @@ -62,14 +62,12 @@ def traces
framework_trace_with_ids = []
full_trace_with_ids = []

if full_trace
full_trace.each_with_index do |trace, idx|
trace_with_id = { id: idx, trace: trace }
full_trace.each_with_index do |trace, idx|
trace_with_id = { id: idx, trace: trace }

appplication_trace_with_ids << trace_with_id if application_trace.include?(trace)
framework_trace_with_ids << trace_with_id if framework_trace.include?(trace)
full_trace_with_ids << trace_with_id
end
appplication_trace_with_ids << trace_with_id if application_trace.include?(trace)
framework_trace_with_ids << trace_with_id if framework_trace.include?(trace)
full_trace_with_ids << trace_with_id
end

{
Expand All @@ -84,19 +82,23 @@ def self.status_code_for_exception(class_name)
end

def source_extract
exception.backtrace.map do |trace|
backtrace.map do |trace|
file, line = trace.split(":")
line_number = line.to_i
{
code: source_fragment(file, line_number),
file: file,
line_number: line_number
}
end if exception.backtrace
end
end

private

def backtrace
Array(@exception.backtrace)
end

def original_exception(exception)
if registered_original_exception?(exception)
exception.original_exception
Expand All @@ -111,9 +113,9 @@ def registered_original_exception?(exception)

def clean_backtrace(*args)
if backtrace_cleaner
backtrace_cleaner.clean(@exception.backtrace, *args)
backtrace_cleaner.clean(backtrace, *args)
else
@exception.backtrace
backtrace
end
end

Expand Down
@@ -1,29 +1,27 @@
<% if @source_extract %>
<% @source_extract.each_with_index do |extract_source, index| %>
<% if extract_source[:code] %>
<div class="source <%="hidden" if @show_source_idx != index%>" id="frame-source-<%=index%>">
<div class="info">
Extracted source (around line <strong>#<%= extract_source[:line_number] %></strong>):
</div>
<div class="data">
<table cellpadding="0" cellspacing="0" class="lines">
<tr>
<td>
<pre class="line_numbers">
<% extract_source[:code].each_key do |line_number| %>
<% @source_extract.each_with_index do |extract_source, index| %>
<% if extract_source[:code] %>
<div class="source <%="hidden" if @show_source_idx != index%>" id="frame-source-<%=index%>">
<div class="info">
Extracted source (around line <strong>#<%= extract_source[:line_number] %></strong>):
</div>
<div class="data">
<table cellpadding="0" cellspacing="0" class="lines">
<tr>
<td>
<pre class="line_numbers">
<% extract_source[:code].each_key do |line_number| %>
<span><%= line_number -%></span>
<% end %>
</pre>
</td>
<% end %>
</pre>
</td>
<td width="100%">
<pre>
<% extract_source[:code].each do |line, source| -%><div class="line<%= " active" if line == extract_source[:line_number] -%>"><%= source -%></div><% end -%>
</pre>
</td>
</tr>
</table>
</div>
</tr>
</table>
</div>
<% end %>
</div>
<% end %>
<% end %>
31 changes: 31 additions & 0 deletions actionpack/test/dispatch/exception_wrapper_test.rb
Expand Up @@ -10,6 +10,12 @@ def initialize(*backtrace)
end
end

class BadlyDefinedError < StandardError
def backtrace
nil
end
end

setup do
Rails.stubs(:root).returns(Pathname.new('.'))

Expand All @@ -28,27 +34,52 @@ def initialize(*backtrace)
assert_equal [ code: 'foo', file: 'lib/file.rb', line_number: 42 ], wrapper.source_extract
end


test '#application_trace returns traces only from the application' do
exception = TestError.new(caller.prepend("lib/file.rb:42:in `index'"))
wrapper = ExceptionWrapper.new(@environment, exception)

assert_equal [ "lib/file.rb:42:in `index'" ], wrapper.application_trace
end

test '#application_trace cannot be nil' do
nil_backtrace_wrapper = ExceptionWrapper.new(@environment, BadlyDefinedError.new)
nil_cleaner_wrapper = ExceptionWrapper.new({}, BadlyDefinedError.new)

assert_equal [], nil_backtrace_wrapper.application_trace
assert_equal [], nil_cleaner_wrapper.application_trace
end

test '#framework_trace returns traces outside the application' do
exception = TestError.new(caller.prepend("lib/file.rb:42:in `index'"))
wrapper = ExceptionWrapper.new(@environment, exception)

assert_equal caller, wrapper.framework_trace
end

test '#framework_trace cannot be nil' do
nil_backtrace_wrapper = ExceptionWrapper.new(@environment, BadlyDefinedError.new)
nil_cleaner_wrapper = ExceptionWrapper.new({}, BadlyDefinedError.new)

assert_equal [], nil_backtrace_wrapper.framework_trace
assert_equal [], nil_cleaner_wrapper.framework_trace
end

test '#full_trace returns application and framework traces' do
exception = TestError.new(caller.prepend("lib/file.rb:42:in `index'"))
wrapper = ExceptionWrapper.new(@environment, exception)

assert_equal exception.backtrace, wrapper.full_trace
end

test '#full_trace cannot be nil' do
nil_backtrace_wrapper = ExceptionWrapper.new(@environment, BadlyDefinedError.new)
nil_cleaner_wrapper = ExceptionWrapper.new({}, BadlyDefinedError.new)

assert_equal [], nil_backtrace_wrapper.full_trace
assert_equal [], nil_cleaner_wrapper.full_trace
end

test '#traces returns every trace by category enumerated with an index' do
exception = TestError.new("lib/file.rb:42:in `index'", "/gems/rack.rb:43:in `index'")
wrapper = ExceptionWrapper.new(@environment, exception)
Expand Down

0 comments on commit 7ae7ae5

Please sign in to comment.