Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions lib/raven/interfaces/stack_trace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,19 @@ class Frame < Interface
def filename
return nil if self.abs_path.nil?

prefix = $LOAD_PATH.select { |s| self.abs_path.start_with?(s.to_s) }.sort_by { |s| s.to_s.length }.last
if prefix.nil? && Raven.configuration.project_root
project_root = Raven.configuration.project_root.to_s
if self.abs_path.start_with?(project_root)
prefix = project_root
end
prefix = if project_root && self.abs_path.start_with?(project_root)
project_root
else
$LOAD_PATH.select { |s| self.abs_path.start_with?(s.to_s) }.sort_by { |s| s.to_s.length }.last
end

prefix ? self.abs_path[prefix.to_s.chomp(File::SEPARATOR).length+1..-1] : self.abs_path
end

def project_root
@project_root ||= Raven.configuration.project_root && Raven.configuration.project_root.to_s
end

def to_hash(*args)
data = super(*args)
data[:filename] = self.filename
Expand Down
20 changes: 17 additions & 3 deletions spec/raven/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@ class SubExc < BaseExc; end
end

context 'in a rails environment' do

before do
rails = double('Rails')
allow(rails).to receive(:root) { '/rails/root' }
Expand All @@ -384,7 +383,7 @@ class SubExc < BaseExc; end
allow(e).to receive(:backtrace).and_return([
"/rails/root/vendor/bundle/cache/other_gem.rb:10:in `public_method'",
"vendor/bundle/some_gem.rb:10:in `a_method'",
"/rails/root/app/foobar:132:in `new_function'",
"/rails/root/app/models/user.rb:132:in `new_function'",
"/gem/lib/path:87:in `a_function'",
"/app/some/other/path:1412:in `other_function'",
"test/some/other/path:1412:in `other_function'"
Expand All @@ -401,13 +400,28 @@ class SubExc < BaseExc; end
expect(frames[1][:in_app]).to eq(false)
expect(frames[2][:filename]).to eq("/gem/lib/path")
expect(frames[2][:in_app]).to eq(false)
expect(frames[3][:filename]).to eq("app/foobar")
expect(frames[3][:filename]).to eq("app/models/user.rb")
expect(frames[3][:in_app]).to eq(true)
expect(frames[4][:filename]).to eq("vendor/bundle/some_gem.rb")
expect(frames[4][:in_app]).to eq(false)
expect(frames[5][:filename]).to eq("vendor/bundle/cache/other_gem.rb")
expect(frames[5][:in_app]).to eq(false)
end

context 'when a path under project_root is on the load path' do
before do
$LOAD_PATH << '/rails/root/app/models'
end

after do
$LOAD_PATH.delete('/rails/root/app/models')
end

it "doesn't remove any path information under project_root" do
frames = hash[:exception][:stacktrace][:frames]
expect(frames[3][:filename]).to eq("app/models/user.rb")
end
end
end
end

Expand Down