Module: Cuprum::ExceptionHandling

Defined in:
lib/cuprum/exception_handling.rb

Overview

Utility module for handling uncaught exceptions in commands.

This functionality can be temporarily disabled by setting the ENV flag; this can be used to debug issues when testing 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) ⇒ 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.

Raises:

  • StandardError if an exception is raised and the ENV flag is set.

See Also:



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cuprum/exception_handling.rb', line 49

def call(*args, **kwargs, &)
  super
rescue StandardError => exception
  raise exception if ENV['CUPRUM_RERAISE_EXCEPTIONS']

  error = Cuprum::Errors::UncaughtException.new(
    exception:,
    message:   "uncaught exception in #{self.class.name} - "
  )
  failure(error)
end