Class: Brainguy::ErrorHandlingNotifier

Inherits:
BasicNotifier show all
Defined in:
lib/brainguy/error_handling_notifier.rb

Overview

It is possible that errors may be raised when notifying listeners. This notifier wrapper class provides some leeway in how to handle those errors. It does this by capturing errors and applying a policy to them.

Includes a selection of common error policies.

Direct Known Subclasses

ErrorCollectingNotifier

Constant Summary collapse

SUPPRESS_STRATEGY =

The suppression strategy. Throws errors away.

->(_subscription, _error) do
  # NOOP
end
WARN_STRATEGY =

The warning strategy. Turns errors into warnings.

->(_subscription, error) do
  warn "#{error.class}: #{error.message}"
end
RAISE_STRATEGY =

The raise strategy. Re-raises errors as if they had never been captured.

->(_subscription, error) do
  raise error
end

Instance Method Summary collapse

Constructor Details

#initialize(notifier, error_handler = RAISE_STRATEGY) ⇒ ErrorHandlingNotifier

Returns a new instance of ErrorHandlingNotifier.

Parameters:

  • notifier (#notify)

    the notifier to wrap

  • error_handler (:call) (defaults to: RAISE_STRATEGY)

    a callable that determined error policy There are some symbolic shortcuts available here:

    • :suppress: Suppress errors completely.
    • :warn: Convert errors to warnings.
    • :raise: Re-raise errors.

31
32
33
34
# File 'lib/brainguy/error_handling_notifier.rb', line 31

def initialize(notifier, error_handler = RAISE_STRATEGY)
  super(notifier)
  @error_handler = resolve_error_handler(error_handler)
end

Instance Method Details

#notify(subscription) ⇒ @Object

Notify a subscription of an event, and apply the error policy to any exception that is raised.

Returns:

  • (@Object)

    whatever the underlying notifier returned


40
41
42
43
44
# File 'lib/brainguy/error_handling_notifier.rb', line 40

def notify(subscription, *)
  super
rescue => error
  @error_handler.call(subscription, error)
end

#resultnil

Returns:

  • (nil)

47
48
49
# File 'lib/brainguy/error_handling_notifier.rb', line 47

def result
  nil
end