Skip to content

Commit

Permalink
Bibliothecary::FileParsingError: add error location to error, and exp…
Browse files Browse the repository at this point in the history
…ose via create_error_analysis (#582)

* Add file location data to FileParsingError messages

e.g. before:
pyproject.toml: no implicit conversion of Hash into Array

e.g. after:
pyproject.toml: no implicit conversion of Hash into Array (parsers/pypi.rb:109:in `+')

* Add specs

* Add an error_message field to the error instead of inside the msg

* 8.7.3
  • Loading branch information
tiegz authored Dec 11, 2023
1 parent 723a767 commit 62175df
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 10 deletions.
5 changes: 3 additions & 2 deletions lib/bibliothecary/analyser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

module Bibliothecary
module Analyser
def self.create_error_analysis(platform_name, relative_path, kind, message)
def self.create_error_analysis(platform_name, relative_path, kind, message, location=nil)
{
platform: platform_name,
path: relative_path,
dependencies: nil,
kind: kind,
success: false,
error_message: message
error_message: message,
error_location: location,
}
end

Expand Down
7 changes: 5 additions & 2 deletions lib/bibliothecary/analyser/analysis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def analyse_contents_from_info(info, options: {})

dependencies_to_analysis(info, kind, dependencies)
rescue Bibliothecary::FileParsingError => e
Bibliothecary::Analyser::create_error_analysis(platform_name, info.relative_path, kind, e.message)
Bibliothecary::Analyser::create_error_analysis(platform_name, info.relative_path, kind, e.message, e.location)
end
alias analyze_contents_from_info analyse_contents_from_info

Expand Down Expand Up @@ -84,7 +84,10 @@ def parse_file(filename, contents, options: {})

rescue Exception => e # default is StandardError but C bindings throw Exceptions
# the C xml parser also puts a newline at the end of the message
raise Bibliothecary::FileParsingError.new(e.message.strip, filename)
location = e.backtrace_locations[0]
.to_s
.then { |l| l =~ /bibliothecary\// ? l.split("bibliothecary/").last : l.split("gems/").last }
raise Bibliothecary::FileParsingError.new(e.message.strip, filename, location)
end

private
Expand Down
5 changes: 3 additions & 2 deletions lib/bibliothecary/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ def initialize(msg, code)
end

class FileParsingError < StandardError
attr_accessor :filename
def initialize(msg, filename)
attr_accessor :filename, :location
def initialize(msg, filename, location=nil)
@filename = filename
@location = location # source code location of the error, e.g. "lib/hi.rb:34"
msg = "#{filename}: #{msg}" unless msg.include?(filename)
super("#{msg}")
end
Expand Down
2 changes: 1 addition & 1 deletion lib/bibliothecary/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Bibliothecary
VERSION = "8.7.2"
VERSION = "8.7.3"
end
3 changes: 2 additions & 1 deletion spec/bibliothecary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@
dependencies: [],
kind: "unknown",
success: false,
error_message: "No parser for this file type" }
error_message: "No parser for this file type",
error_location: nil }
])

Bibliothecary.reset
Expand Down
3 changes: 2 additions & 1 deletion spec/parsers/maven_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@
dependencies: nil,
kind: 'lockfile',
success: false,
error_message: "missing_info.xml: ivy-report document lacks <info> element"
error_message: "missing_info.xml: ivy-report document lacks <info> element",
error_location: "parsers/maven.rb:134:in `parse_ivy_report'"
})
end

Expand Down
3 changes: 2 additions & 1 deletion spec/parsers/npm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@
dependencies: nil,
kind: 'manifest',
success: false,
error_message: "package.json: appears to be a lockfile rather than manifest format"
error_message: "package.json: appears to be a lockfile rather than manifest format",
error_location: "parsers/npm.rb:97:in `parse_manifest'"
})
end

Expand Down

0 comments on commit 62175df

Please sign in to comment.