Class: BarsoomUtils::ExceptionNotifier

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

Class Method Summary collapse

Class Method Details

.message(message, details_or_context = nil, context_or_nothing = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/barsoom_utils/exception_notifier.rb', line 21

def self.message(message, details_or_context = nil, context_or_nothing = nil)
  ensure_notifier!

  if context_or_nothing
    details = details_or_context
    context = context_or_nothing
  elsif details_or_context.is_a?(Hash)
    details = nil
    context = details_or_context
  else
    details = details_or_context
    context = {}
  end

  if defined?(Honeybadger)
    result = Honeybadger.notify(
      error_class: message,
      error_message: (details || "(no message)").to_s,
      context: context.to_h,
    )
  end

  if defined?(Sentry)
    title = details ? "#{message}: #{details}" : message.to_s
    Sentry.capture_message(title, extra: context.to_h)
  end

  result
end

.notify(exception, context: {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/barsoom_utils/exception_notifier.rb', line 7

def self.notify(exception, context: {})
  # Inelegant workaround for the fact that we've confused this method with .message at least once.
  # TODO: Fold them into a single method?
  unless exception.is_a?(Exception)
    raise "Expected an exception but got: #{exception.inspect}"
  end

  ensure_notifier!

  result = Honeybadger.notify(exception, context: context) if defined?(Honeybadger)
  Sentry.capture_exception(exception, extra: context) if defined?(Sentry)
  result
end