Class: ExceptionConvertingDelegator

Inherits:
ExceptionCatchingDelegator show all
Defined in:
lib/akephalos/exception_handling_delegators.rb

Overview

This class can be used to wrap an object so that exceptions matching a given type are rescued and then raised as another type.

This kind of exception converting is most of use when dealing with exceptions passed across DRb where a remote exception class may not exist on the client-side.

Instance Method Summary collapse

Methods inherited from ExceptionCatchingDelegator

#method_missing

Constructor Details

#initialize(delegate, exception_type_to_catch = Exception, exception_type_to_throw = RuntimeError) ⇒ ExceptionConvertingDelegator

  • Args :

    • ++ -> delgate - object to be wrapped

    • ++ -> exception_type_to_catch - an exception class or name of an exception class that will be used to match (in a regular-expression sense) the name of exceptions thrown by the delegate’s methods

    • ++ -> exception_type_to_throw - the exception class that will be used to create and raise a new exception



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/akephalos/exception_handling_delegators.rb', line 45

def initialize(delegate, exception_type_to_catch = Exception, exception_type_to_throw = RuntimeError)

  handler = lambda do |e|
    
    raise e unless e.class.name =~ Regexp.new(exception_type_to_catch.to_s)
    
    # Create and raise a RuntimeError
    message = e.class.name
    unless e.message.nil? || e.message.size == 0
      message << " "
      message << e.message
    end
    new_exception = exception_type_to_throw.new(message)
    new_exception.set_backtrace(e.backtrace)
    raise new_exception
  end

  super(delegate, handler)

end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ExceptionCatchingDelegator