class Mihari::Notifiers::ExceptionNotifier

Public Class Methods

new() click to toggle source
# File lib/mihari/notifiers/exception_notifier.rb, line 8
def initialize
  @backtrace_lines = 10
  @color = "danger"

  @slack = Notifiers::Slack.new
end

Public Instance Methods

format_backtrace(backtrace) click to toggle source

Format backtrace in string

@param [Array] backtrace

@return [String]

# File lib/mihari/notifiers/exception_notifier.rb, line 121
def format_backtrace(backtrace)
  return nil unless backtrace

  "```#{backtrace.first(@backtrace_lines).join("\n")}```"
end
hostname() click to toggle source

Hostname of runnning instance

@return [String]

# File lib/mihari/notifiers/exception_notifier.rb, line 108
def hostname
  Socket.gethostname
rescue StandardError => _e
  "N/A"
end
notify(exception) click to toggle source
# 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:) click to toggle source

Send notification to Slack

@param [String] text @param [Array<Hash>] attachments

@return [nil]

# File lib/mihari/notifiers/exception_notifier.rb, line 35
def notify_to_slack(text:, attachments:)
  @slack.notify(text: text, attachments: attachments)
end
notify_to_stdout(exception) click to toggle source

Send notification to STDOUT

@param [Exception] exception

@return [nil]

# File lib/mihari/notifiers/exception_notifier.rb, line 46
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) click to toggle source

Convert exception to attachments (for Slack)

@param [Exception] exception @param [String] clean_message

@return [Array<Hash>]

# File lib/mihari/notifiers/exception_notifier.rb, line 61
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) click to toggle source

Convert clean_message and backtrace into fields (for Slack)

@param [String] clean_message @param [Array] backtrace

@return [Array<Hash>]

# File lib/mihari/notifiers/exception_notifier.rb, line 90
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) click to toggle source

Convert exception class to text

@param [Class<Exception>] exception_class

@return [String]

# File lib/mihari/notifiers/exception_notifier.rb, line 76
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?() click to toggle source
# File lib/mihari/notifiers/exception_notifier.rb, line 15
def valid?
  @slack.valid?
end