Class: Mihari::Notifiers::ExceptionNotifier
- Inherits:
-
Object
- Object
- Mihari::Notifiers::ExceptionNotifier
- Defined in:
- lib/mihari/notifiers/exception_notifier.rb
Instance Method Summary collapse
- #format_backtrace(backtrace) ⇒ Object
- #hostname ⇒ Object
-
#initialize ⇒ ExceptionNotifier
constructor
A new instance of ExceptionNotifier.
- #notify(exception) ⇒ Object
- #notify_to_slack(text:, attachments:) ⇒ Object
- #notify_to_stdout(exception) ⇒ Object
- #to_attachments(exception, clean_message) ⇒ Object
- #to_fields(clean_message, backtrace) ⇒ Object
- #to_text(exception_class) ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize ⇒ ExceptionNotifier
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 |
#hostname ⇒ Object
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 = exception..tr('`', "'") = (exception, ) notify_to_slack(text: , 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: ) 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 = "#{text}: #{exception.}" puts 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 (exception, ) text = to_text(exception.class) backtrace = exception.backtrace fields = to_fields(, 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(, backtrace) fields = [ { title: "Exception", value: }, { 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
15 16 17 |
# File 'lib/mihari/notifiers/exception_notifier.rb', line 15 def valid? @slack.valid? end |