Skip to content

Commit

Permalink
Mark file paths beginning with 'vendor' as not in project
Browse files Browse the repository at this point in the history
Fixes #394
  • Loading branch information
kattrali committed Nov 18, 2017
1 parent dd90cb5 commit 7323b6a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/bugsnag/stacktrace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class Stacktrace
# e.g. "org.jruby.Ruby.runScript(Ruby.java:807)"
JAVA_BACKTRACE_REGEX = /^(.*)\((.*)(?::([0-9]+))?\)$/

# Path to vendored code. Used to mark file paths as out of project.
VENDOR_PATH = 'vendor/'

def initialize(backtrace, configuration)
@configuration = configuration

Expand All @@ -31,7 +34,6 @@ def initialize(backtrace, configuration)

# Generate the stacktrace line hash
trace_hash = {}
trace_hash[:inProject] = true if in_project?(file)
trace_hash[:lineNumber] = line_str.to_i

if configuration.send_code
Expand All @@ -40,9 +42,12 @@ def initialize(backtrace, configuration)

# Clean up the file path in the stacktrace
if defined?(@configuration.project_root) && @configuration.project_root.to_s != ''
trace_hash[:inProject] = true if file.start_with?(@configuration.project_root)
file.sub!(/#{@configuration.project_root}\//, "")
trace_hash.delete(:inProject) if file.start_with?(VENDOR_PATH)
end


# Strip common gem path prefixes
if defined?(Gem)
file = Gem.path.inject(file) {|line, path| line.sub(/#{path}\//, "") }
Expand All @@ -67,10 +72,6 @@ def to_a

private

def in_project?(line)
@configuration.project_root && line.start_with?(@configuration.project_root.to_s)
end

def code(file, line_number, num_lines = 7)
code_hash = {}

Expand Down
28 changes: 28 additions & 0 deletions spec/report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,34 @@ def gloops
}
end

it 'marks vendored stack frames as out-of-project' do
project_root = File.expand_path File.dirname(__FILE__)
Bugsnag.configuration.project_root = project_root

ex = Exception.new('Division by zero')
allow(ex).to receive (:backtrace) {[
File.join(project_root, "vendor/strutils/lib/string.rb:508:in `splice'"),
File.join(project_root, "vendors/strutils/lib/string.rb:508:in `splice'"),
File.join(project_root, "lib/helpers/string.rb:32:in `splice'"),
File.join(project_root, "lib/vendor/lib/article.rb:158:in `initialize'"),
File.join(project_root, "lib/prog.rb:158:in `read_articles'"),
"app.rb:10:in `main'",
"(pry):3:in `__pry__'"
]}
Bugsnag.notify(ex)
expect(Bugsnag).to have_sent_notification{ |payload|
exception = get_exception_from_payload(payload)

expect(exception["stacktrace"][0]["inProject"]).to be_nil
expect(exception["stacktrace"][1]["inProject"]).to be true
expect(exception["stacktrace"][2]["inProject"]).to be true
expect(exception["stacktrace"][3]["inProject"]).to be true
expect(exception["stacktrace"][4]["inProject"]).to be true
expect(exception["stacktrace"][5]["inProject"]).to be_nil
expect(exception["stacktrace"][6]["inProject"]).to be_nil
}
end

it "adds app_version to the payload if it is set" do
Bugsnag.configuration.app_version = "1.1.1"
Bugsnag.notify(BugsnagTestException.new("It crashed"))
Expand Down

0 comments on commit 7323b6a

Please sign in to comment.