Class: Failbot::ExceptionFormat::Haystack

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

Overview

The format recognized by haystack.

Class Method Summary collapse

Class Method Details

.call(e) ⇒ Object

Format an exception.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/failbot/exception_format/haystack.rb', line 7

def self.call(e)
  res = {
    'class'      => e.class.to_s,
    'message'    => e.message,
    'backtrace'  => Array(e.backtrace)[0,500].join("\n"),
    'ruby'       => RUBY_DESCRIPTION,
    'created_at' => Time.now.utc.iso8601(6)
  }

  if cause = pretty_print_cause(e)
    res['cause'] = cause
  end

  res
end

.exception_classname_from_hash(hash) ⇒ Object

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



29
30
31
# File 'lib/failbot/exception_format/haystack.rb', line 29

def self.exception_classname_from_hash(hash)
  hash["class"]
end

.exception_message_from_hash(hash) ⇒ Object

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



24
25
26
# File 'lib/failbot/exception_format/haystack.rb', line 24

def self.exception_message_from_hash(hash)
  hash["message"]
end

.pretty_print_cause(e, depth = MAXIMUM_CAUSE_DEPTH) ⇒ Object

Pretty-print Exception#cause (and nested causes) for inclusion in needle context

e - The Exception object whose #cause should be printed depth - Integer number of Exception#cause objects to descend into.

Returns a String.



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

def self.pretty_print_cause(e, depth = MAXIMUM_CAUSE_DEPTH)
  return unless depth > 0

  causes = []

  current = e
  depth.times do
    pretty_cause = pretty_print_one_cause(current)
    break unless pretty_cause
    causes << pretty_cause
    current = current.cause
  end

  return if causes.empty?

  result = causes.join("\n\nCAUSED BY:\n\n")

  if current.cause
    result << "\n\nFurther #cause backtraces were omitted\n"
  end

  result
end

.pretty_print_one_cause(e) ⇒ Object

Pretty-print a single Exception#cause

e - The Exception object whose #cause should be printed

Returns a String.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/failbot/exception_format/haystack.rb', line 69

def self.pretty_print_one_cause(e)
  cause = e.cause
  return unless cause

  result = "#{cause.class.name}: #{cause.message}\n"

  # Find where the cause's backtrace differs from the child exception's.
  backtrace = Array(e.backtrace)
  cause_backtrace = Array(cause.backtrace)
  index = -1
  min_index = [backtrace.size, cause_backtrace.size].min * -1
  just_in_case = -5000

  while index > min_index && backtrace[index] == cause_backtrace[index] && index >= just_in_case
    index -= 1
  end

  # Add on a few common frames to make it clear where the backtraces line up.
  index += 3
  index = -1 if index >= 0

  cause_backtrace[0..index].each do |line|
    result << "\t#{line}\n"
  end

  result
end