Skip to content

Commit

Permalink
don't parse backtrace strings for file and line info
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlie Somerville committed Jan 12, 2013
1 parent 90215ca commit fe77459
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
26 changes: 13 additions & 13 deletions lib/better_errors/stack_frame.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ module BetterErrors
# @private
class StackFrame
def self.from_exception(exception)
idx_offset = 0
list = exception.backtrace.each_with_index.map do |frame, idx|
frame_binding = exception.__better_errors_bindings_stack[idx - idx_offset]
next unless md = /\A(?<file>.*?):(?<line>\d+)(:in `(?<name>.*)')?/.match(frame)

# prevent mismatching frames in the backtrace with the binding stack
if frame_binding and frame_binding.eval("__FILE__") != md[:file]
idx_offset += 1
frame_binding = nil
end

StackFrame.new(md[:file], md[:line].to_i, md[:name], frame_binding)
end.compact
if exception.__better_errors_bindings_stack.any?
list = exception.__better_errors_bindings_stack.map { |binding|
file = binding.eval "__FILE__"
line = binding.eval "__LINE__"
name = binding.frame_description
StackFrame.new(file, line, name, binding)
}
else
list = (exception.backtrace || []).map { |frame|
next unless md = /\A(?<file>.*?):(?<line>\d+)(:in `(?<name>.*)')?/.match(frame)
StackFrame.new(md[:file], md[:line].to_i, md[:name])
}.compact
end

if exception.is_a?(SyntaxError) && exception.to_s =~ /\A(.*):(\d*):/
list.unshift StackFrame.new($1, $2.to_i, "")
Expand Down
11 changes: 5 additions & 6 deletions spec/better_errors/stack_frame_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,15 @@ module BetterErrors
end

it "should special case SyntaxErrors" do
syntax_error = SyntaxError.new "my_file.rb:123: you wrote bad ruby!"
syntax_error.stub!(:backtrace).and_return([])
syntax_error = SyntaxError.allocate
Exception.instance_method(:initialize).bind(syntax_error).call("my_file.rb:123: you wrote bad ruby!")
frames = StackFrame.from_exception(syntax_error)
frames.count.should == 1
frames.first.filename.should == "my_file.rb"
frames.first.line.should == 123
end

it "should not blow up if no method name is given" do
error = StandardError.new
error = StandardError.allocate

error.stub!(:backtrace).and_return(["foo.rb:123"])
frames = StackFrame.from_exception(error)
Expand All @@ -105,14 +104,14 @@ module BetterErrors
end

it "should ignore a backtrace line if its format doesn't make any sense at all" do
error = StandardError.new
error = StandardError.allocate
error.stub!(:backtrace).and_return(["foo.rb:123:in `foo'", "C:in `find'", "bar.rb:123:in `bar'"])
frames = StackFrame.from_exception(error)
frames.count.should == 2
end

it "should not blow up if a filename contains a colon" do
error = StandardError.new
error = StandardError.allocate
error.stub!(:backtrace).and_return(["crap:filename.rb:123"])
frames = StackFrame.from_exception(error)
frames.first.filename.should == "crap:filename.rb"
Expand Down

0 comments on commit fe77459

Please sign in to comment.