module Serf::Util::ErrorHandling

Helper module to rescues exceptions from executing blocks of code, and then converts the exception to an “Error Message”.

Public Instance Methods

handle_error(e) click to toggle source

Including classes may override this method to do alternate error handling. By default, this method will create a new error event message.

# File lib/serf/util/error_handling.rb, line 27
def handle_error(e)
  # no error was passed, so do nothing.
  return nil unless e

  # Return a simple error event message
  return {
    error: e.class.to_s,
    message: e.message,
    process_env: ENV.to_hash,
    hostname: Socket.gethostname,
    backtrace: e.backtrace.join("\n")
  }
end
with_error_handling(*args, &block) click to toggle source

A block wrapper to handle errors when executing a block.

# File lib/serf/util/error_handling.rb, line 18
def with_error_handling(*args, &block)
  results, err = pcall *args, &block
  return results, handle_error(err)
end