Class: Mihari::Notifiers::ExceptionNotifier

Inherits:
Object
  • Object
show all
Defined in:
lib/mihari/notifiers/exception_notifier.rb

Instance Method Summary collapse

Constructor Details

#initializeExceptionNotifier

Returns a new instance of ExceptionNotifier.



8
9
10
11
12
13
# File 'lib/mihari/notifiers/exception_notifier.rb', line 8

def initialize
  @backtrace_lines = 10
  @color = "danger"

  @slack = Notifiers::Slack.new
end

Instance Method Details

#format_backtrace(backtrace) ⇒ Object



71
72
73
74
75
# File 'lib/mihari/notifiers/exception_notifier.rb', line 71

def format_backtrace(backtrace)
  return nil unless backtrace

  "```#{backtrace.first(@backtrace_lines).join("\n")}```"
end

#hostnameObject



65
66
67
68
69
# File 'lib/mihari/notifiers/exception_notifier.rb', line 65

def hostname
  Socket.gethostname
rescue StandardError => _e
  "N/A"
end

#notify(exception) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/mihari/notifiers/exception_notifier.rb', line 19

def notify(exception)
  notify_to_stdout exception

  clean_message = exception.message.tr('`', "'")
  attachments = to_attachments(exception, clean_message)
  notify_to_slack(text: clean_message, attachments: attachments) if @slack.valid?
end

#notify_to_slack(text:, attachments:) ⇒ Object



27
28
29
# File 'lib/mihari/notifiers/exception_notifier.rb', line 27

def notify_to_slack(text:, attachments:)
  @slack.notify(text: text, attachments: attachments)
end

#notify_to_stdout(exception) ⇒ Object



31
32
33
34
35
36
# File 'lib/mihari/notifiers/exception_notifier.rb', line 31

def notify_to_stdout(exception)
  text = to_text(exception.class).chomp
  message = "#{text}: #{exception.message}"
  puts message
  puts format_backtrace(exception.backtrace) if exception.backtrace
end

#to_attachments(exception, clean_message) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/mihari/notifiers/exception_notifier.rb', line 38

def to_attachments(exception, clean_message)
  text = to_text(exception.class)
  backtrace = exception.backtrace
  fields = to_fields(clean_message, backtrace)

  [color: @color, text: text, fields: fields, mrkdwn_in: %w[text fields]]
end

#to_fields(clean_message, backtrace) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/mihari/notifiers/exception_notifier.rb', line 52

def to_fields(clean_message, backtrace)
  fields = [
    { title: "Exception", value: clean_message },
    { title: "Hostname", value: hostname }
  ]

  if backtrace
    formatted_backtrace = format_backtrace(backtrace)
    fields << { title: "Backtrace", value: formatted_backtrace }
  end
  fields
end

#to_text(exception_class) ⇒ Object



46
47
48
49
50
# File 'lib/mihari/notifiers/exception_notifier.rb', line 46

def to_text(exception_class)
  measure_word = /^[aeiou]/i.match?(exception_class.to_s) ? "An" : "A"
  exception_name = "*#{measure_word}* `#{exception_class}`"
  "#{exception_name} *occured in background*\n"
end

#valid?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/mihari/notifiers/exception_notifier.rb', line 15

def valid?
  @slack.valid?
end