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


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.


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


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

def result
  nil
end