From cc80f8610fbd893a22f373206f37957ef5471346 Mon Sep 17 00:00:00 2001 From: Charlie Somerville Date: Tue, 11 Dec 2012 20:55:33 +1100 Subject: [PATCH] don't assume "file:line:in `method`" format for backtrace lines (#28) --- lib/better_errors/stack_frame.rb | 2 +- spec/better_errors/stack_frame_spec.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/better_errors/stack_frame.rb b/lib/better_errors/stack_frame.rb index 79ab56fe..953294e2 100644 --- a/lib/better_errors/stack_frame.rb +++ b/lib/better_errors/stack_frame.rb @@ -4,7 +4,7 @@ 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] - md = /\A(?.*):(?\d*):in `(?.*)'\z/.match(frame) + md = /\A(?.*):(?\d*)(:in `(?.*)')?\z/.match(frame) # prevent mismatching frames in the backtrace with the binding stack if frame_binding and frame_binding.eval("__FILE__") != md[:file] diff --git a/spec/better_errors/stack_frame_spec.rb b/spec/better_errors/stack_frame_spec.rb index 0ae640c6..b7b0a770 100644 --- a/spec/better_errors/stack_frame_spec.rb +++ b/spec/better_errors/stack_frame_spec.rb @@ -66,5 +66,19 @@ module BetterErrors 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.stub!(:backtrace).and_return(["foo.rb:123"]) + frames = StackFrame.from_exception(error) + frames.first.filename.should == "foo.rb" + frames.first.line.should == 123 + + error.stub!(:backtrace).and_return(["foo.rb:123: this is an error message"]) + frames = StackFrame.from_exception(error) + frames.first.filename.should == "foo.rb" + frames.first.line.should == 123 + end end end