Class: BarsoomUtils::ExceptionNotifier

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

Class Method Summary collapse

Class Method Details

.developers_working_on_this_feature_are_responsible_for_errors_until(expire_on, &block) ⇒ Object

While developing a feature we’d like the feature developers to be responsible for any errors that occur. Wrapping the new code with this tags the errors as “wip” in order to hide them from the dashboard.



52
53
54
55
56
57
58
59
60
# File 'lib/barsoom_utils/exception_notifier.rb', line 52

def self.developers_working_on_this_feature_are_responsible_for_errors_until(expire_on, &block)
  block.call
rescue => ex
  FIXME "#{expire_on}: WIP error-handling code needs to be removed!"
  notify(ex, context: { tags: "wip" })

  is_rails_production = defined?(Rails) && Rails.env.production?
  raise unless is_rails_production
end

.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