lib/lopata/observers/backtrace_formatter.rb in lopata-0.1.5 vs lib/lopata/observers/backtrace_formatter.rb in lopata-0.1.6
- old
+ new
@@ -1,9 +1,13 @@
module Lopata
module Observers
+ # @private
# Based on RSpec::Core::BacktraceFormatter
+ #
+ # Provides ability to format backtrace and find source code by file and line number.
class BacktraceFormatter
+ # @private
attr_accessor :exclusion_patterns, :inclusion_patterns
def initialize
patterns = %w[ /lib\d*/ruby/ bin/ exe/lopata /lib/bundler/ /exe/bundle /\.rvm/ ]
patterns.map! { |s| Regexp.new(s.gsub("/", File::SEPARATOR)) }
@@ -12,10 +16,14 @@
@inclusion_patterns = []
inclusion_patterns << Regexp.new(Dir.getwd)
end
+ # Filter backtrace.
+ #
+ # @param backtrace [Array<String>] exception backtrace
+ # @return [Array<String>] backtrace lines except ruby libraries, gems and executable files.
def format(backtrace)
return [] unless backtrace
return backtrace if backtrace.empty?
backtrace.map { |l| backtrace_line(l) }.compact.
@@ -26,9 +34,15 @@
filtered << " Showing full backtrace because every line was filtered out."
end
end
end
+
+ # Extracts error message from excetion
+ #
+ # @param exeption [Exception]
+ # @param include_backtrace [Boolean] whether to add formatted backtrace to output
+ # @return [String] error message from excetion, incuding source code line.
def error_message(exception, include_backtrace: false)
backtrace = format(exception.backtrace)
source_line = extract_source_line(backtrace.first)
msg = ''
msg << "\n#{source_line}\n" if source_line