Module: Bugstack::Fingerprint
- Defined in:
- lib/bugstack/fingerprint.rb
Class Method Summary collapse
-
.extract_location(exception) ⇒ Array<(String, String, String, Integer)>
Extract file, function, and line from an exception’s backtrace.
-
.format_backtrace(exception) ⇒ String
Format an exception’s backtrace as a string.
-
.generate(exception_type, file, function, line = nil) ⇒ String
Generate a stable SHA-256 fingerprint for an error.
Class Method Details
.extract_location(exception) ⇒ Array<(String, String, String, Integer)>
Extract file, function, and line from an exception’s backtrace.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/bugstack/fingerprint.rb', line 26 def self.extract_location(exception) exception_type = exception.class.name bt = exception.backtrace return [exception_type, "", "", nil] if bt.nil? || bt.empty? # Parse the first backtrace line: "file:line:in `method'" first_line = bt.first if first_line =~ /\A(.+):(\d+):in [`'](.+)'\z/ file = Regexp.last_match(1) line = Regexp.last_match(2).to_i function = Regexp.last_match(3) [exception_type, file, function, line] else [exception_type, first_line, "", nil] end end |
.format_backtrace(exception) ⇒ String
Format an exception’s backtrace as a string.
48 49 50 51 52 53 54 55 |
# File 'lib/bugstack/fingerprint.rb', line 48 def self.format_backtrace(exception) bt = exception.backtrace return "#{exception.class}: #{exception.}" if bt.nil? || bt.empty? lines = ["#{exception.class}: #{exception.}"] bt.each { |frame| lines << " from #{frame}" } lines.join("\n") end |
.generate(exception_type, file, function, line = nil) ⇒ String
Generate a stable SHA-256 fingerprint for an error.
14 15 16 17 18 19 |
# File 'lib/bugstack/fingerprint.rb', line 14 def self.generate(exception_type, file, function, line = nil) parts = [exception_type, file, function] parts << line.to_s if line key = parts.join(":") Digest::SHA256.hexdigest(key)[0, 16] end |