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
14 changes: 12 additions & 2 deletions lib/raven/interfaces/stack_trace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,29 @@ def initialize(*arguments)
def filename
return nil if self.abs_path.nil?

prefix = if project_root && self.abs_path.start_with?(project_root)
prefix = if under_project_root? && in_app
project_root
elsif under_project_root?
longest_load_path || project_root
else
$LOAD_PATH.select { |s| self.abs_path.start_with?(s.to_s) }.sort_by { |s| s.to_s.length }.last
longest_load_path
end

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

def under_project_root?
project_root && abs_path.start_with?(project_root)
end

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

def longest_load_path
$LOAD_PATH.select { |s| self.abs_path.start_with?(s.to_s) }.sort_by { |s| s.to_s.length }.last
end

def to_hash(*args)
data = super(*args)
data[:filename] = self.filename
Expand Down
26 changes: 24 additions & 2 deletions spec/raven/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ class SubExc < BaseExc; end
expect(frames[5][:in_app]).to eq(false)
end

context 'when a path under project_root is on the load path' do
context 'when an in_app path under project_root is on the load path' do
before do
$LOAD_PATH << '/rails/root/app/models'
end
Expand All @@ -458,11 +458,33 @@ class SubExc < BaseExc; end
$LOAD_PATH.delete('/rails/root/app/models')
end

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

context 'when a non-in_app path under project_root is on the load path' do
before do
$LOAD_PATH << '/rails/root/vendor/bundle'
end

after do
$LOAD_PATH.delete('/rails/root/vendor/bundle')
end

it 'normalizes the filename using the load path' do
frames = hash[:exception][:values][0][:stacktrace][:frames]
expect(frames[5][:filename]).to eq("cache/other_gem.rb")
end
end

context "when a non-in_app path under project_root isn't on the load path" do
it 'normalizes the filename using project_root' do
frames = hash[:exception][:values][0][:stacktrace][:frames]
expect(frames[5][:filename]).to eq("vendor/bundle/cache/other_gem.rb")
end
end
end
end

Expand Down