Class: Failbot::ExceptionFormat::Structured

Inherits:
Object
  • Object
show all
Defined in:
lib/failbot/exception_format/structured.rb

Overview

A newer exception format , based on the one sentry uses. Aside from different names and locations for things, the notable difference from haystack is that backtrace data has more structure.

Constant Summary collapse

EMPTY_ARRAY =
[].freeze
FURTHER_CAUSES_WERE_OMITTED =
{
  "type" => "Notice",
  "value" => "further Exception#cause values were omitted",
  "stacktrace" => EMPTY_ARRAY
}.freeze

Class Method Summary collapse

Class Method Details

.call(e) ⇒ Object

Format an exception.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/failbot/exception_format/structured.rb', line 14

def self.call(e)
  message = e.message.to_s
  class_name = e.class.to_s
  begin
    Failbot.backtrace_parser.call(e)
  rescue => ex
    message += "\nUnable to parse backtrace (#{ex.inspect})\nDon't put non-backtrace text in Exception#backtrace please!\nSo-called backtrace follows:\n#{e.backtrace.join("\n")}"
    class_name += " (backtrace failed to parse)"
    EMPTY_ARRAY
  end
  {
    "platform" => "ruby",
    "exception_detail" => exception_details(e),
    "ruby" => RUBY_DESCRIPTION,
    "created_at" => Time.now.utc.iso8601(6)
  }
end

.exception_classname_from_hash(hash) ⇒ Object

given a hash generated by this class, return the exception class name.



73
74
75
# File 'lib/failbot/exception_format/structured.rb', line 73

def self.exception_classname_from_hash(hash)
  hash.dig("exception_detail", 0, "type")
end

.exception_details(e) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/failbot/exception_format/structured.rb', line 38

def self.exception_details(e)
  result = []
  depth = 0

  loop do
    message = e.message.to_s
    class_name = e.class.to_s
    stacktrace = begin
      Failbot.backtrace_parser.call(e)
    rescue => ex
      message += "\nUnable to parse backtrace (#{ex.inspect})\nDon't put non-backtrace text in Exception#backtrace please!\nSo-called backtrace follows:\n#{e.backtrace.join("\n")}"
      class_name += " (backtrace failed to parse)"
      EMPTY_ARRAY
    end
    result.unshift({
      "type" => class_name,
      "value" => message,
      "stacktrace" => stacktrace
    })
    depth += 1
    break unless (e=e.cause)
    if depth > MAXIMUM_CAUSE_DEPTH
      result.unshift(FURTHER_CAUSES_WERE_OMITTED)
      break
    end
  end
  result
end

.exception_message_from_hash(hash) ⇒ Object

given a hash generated by this class, return the exception message.



68
69
70
# File 'lib/failbot/exception_format/structured.rb', line 68

def self.exception_message_from_hash(hash)
  hash.dig("exception_detail", 0, "value")
end