Module: Serf::Util::WithErrorHandling

Included in:
Runners::DirectRunner, Serfer
Defined in:
lib/serf/util/with_error_handling.rb

Overview

Helper module to rescues exceptions from executing blocks of code, and then logs+publishes the error event.

Instance Method Summary collapse

Instance Method Details

#with_error_handling(context = nil) ⇒ Object

A block wrapper to handle errors when executing a block.

Including classes may have the following instance variables to override the default values:

  • @error_event_class - ::Serf::Messages::CaughtExceptionEvent

  • @logger - ::Serf::Util::NullObject.new

  • @error_channel - ::Serf::Util::NullObject.new



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/serf/util/with_error_handling.rb', line 22

def with_error_handling(context=nil)
  yield
rescue => e
  eec = @error_event_class || ::Serf::Messages::CaughtExceptionEvent
  logger = @logger || ::Serf::Util::NullObject.new
  error_channel = @error_channel || ::Serf::Util::NullObject.new
  error_event = eec.new(
    context: context,
    error_message: e.inspect,
    error_backtrace: e.backtrace.join("\n"))

  # log the error to our logger, and to our error channel.
  logger.error error_event
  error_channel.publish error_event

  # We're done, so just return this error.
  return error_event
end