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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/barsoom_utils/exception_notifier.rb', line 17

def self.message(message, details_or_context = nil, context_or_nothing = nil)
  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

  details ||= "(no message)"

  Honeybadger.notify(
    error_class: message,
    error_message: details.to_s,
    context: context.to_h,
  )
end

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



7
8
9
10
11
12
13
14
15
# 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

  Honeybadger.notify(exception, context: context)
end

.run_with_context(context, &block) ⇒ Object

Wrap this around code to add context when reporting errors.



39
40
41
42
43
44
45
46
47
48
# File 'lib/barsoom_utils/exception_notifier.rb', line 39

def self.run_with_context(context, &block)
  # The load/dump achieves a "deep dup" without the "deep dep" of Active Support 🥁
  old_context = Marshal.load(Marshal.dump(Honeybadger.get_context))

  Honeybadger.context(context)
  block.call
ensure
  Honeybadger.context.clear!
  Honeybadger.context(old_context)
end