Module: Cuprum::ExceptionHandling

Defined in:
lib/cuprum/exception_handling.rb

Overview

Utility module for handling uncaught exceptions in commands.

Examples:

class UnsafeCommand < Cuprum::Command
  private

  def process
    raise 'Something went wrong.'
  end
end

class SafeCommand < UnsafeCommand
  include Cuprum::ExceptionHandling
end

UnsafeCommand.new.call
#=> raises a StandardError

result = SafeCommand.new.call
#=> a Cuprum::Result
result.error
#=> a Cuprum::Errors::UncaughtException error.
result.error.message
#=> 'uncaught exception in SafeCommand -' \
#   ' StandardError: Something went wrong.'

Instance Method Summary collapse

Instance Method Details

#call(*args, **kwargs, &block) ⇒ Cuprum::Result

Wraps the #call method with a rescue clause matching any StandardError.

If a StandardError or subclass thereof is raised and not caught by #call, then ExceptionHandling will rescue the exception and return a failing Cuprum::Result with a Cuprum::Errors::UncaughtException error.

Returns:

  • (Cuprum::Result)

    the result of calling the superclass method, or a failing result if a StandardError is raised.

See Also:



42
43
44
45
46
47
48
49
50
# File 'lib/cuprum/exception_handling.rb', line 42

def call(*args, **kwargs, &block)
  super
rescue StandardError => exception
  error = Cuprum::Errors::UncaughtException.new(
    exception: exception,
    message:   "uncaught exception in #{self.class.name} - "
  )
  failure(error)
end