Module: ErrorNotifier

Defined in:
lib/error_notifier.rb,
lib/error_notifier/version.rb

Overview

Error Notifier

Central points for all of your Error notification

Examples:

Adding multiple notifiers


ErrorNotifier.add_notifier(:newrelic) do |exception, _options|
  NewRelic::Agent.notice_error(e)
end
ErrorNotifier.add_notifier(:honeybadger) do |exception, options|
  Honeybadger.notify(exception, options)
end
ErrorNotifier.add_notifier(:bugsnag) do |exception|
  Bugsnag.notify(exception)
end
ErrorNotifier.add_notifier(:kernel) do |exception, options|
  Kernel.puts(exception)
end

# ... later ...

begin
  # something that raises an error
rescue => e
  ErrorNotifier.notify(e)
end

Author:

  • Franky W.

Defined Under Namespace

Classes: InvalidNotiferError, Version

Constant Summary

VERSION =

The current version of ErrorNotifer

Version.to_s

Class Method Summary collapse

Class Method Details

.add_notifier(name, &block) ⇒ nil

Add a notifier to the list of registed notifications

Examples:

Add HoneyBadger to the list of Error Notification tools

add_notifier(:honeybadger) do |exception, options|
  Honeybadger.notice(exception, options)
end

Parameters:

  • name (Symbol)

    Name that will be associated with the notifier

  • &block (Proc)

    Block that will be called with the exception an options

Returns:

  • (nil)

Raises:



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/error_notifier.rb', line 61

def add_notifier(name, &block)
  @notifiers = {} unless defined? @notifiers
  if block_given?
    @notifiers[name] = block
  else
    unless (name.respond_to? :call) && (name.method(:call).arity == 2)
      raise InvalidNotiferError "When passing object #{name} to the ErrorNotifier, it must respond to `call(e, options)`"
    end
    @notifiers[name.class.to_s] = name
  end
end

.delete_notifier(name) ⇒ Notifer

Deletes a notifier from the registered notification tools

Parameters:

  • name (Symbol)

    Name with which the notification was regisitered intiially

Returns:

  • (Notifer)


76
77
78
# File 'lib/error_notifier.rb', line 76

def delete_notifier(name)
  @notifiers.delete(name) if defined? @notifiers
end

.notify(e, options = {}) ⇒ nil

Actually send out the notifications to all the registered services

Returns:

  • (nil)


42
43
44
45
46
47
48
# File 'lib/error_notifier.rb', line 42

def notify(e, options = {})
  @notifiers = {} unless defined? @notifiers
  @notifiers.each do |_name, notifier|
    notifier.call(e, options)
  end
  nil
end