Module: Genius::Errors::DynamicRescue

Defined in:
lib/genius/api/errors.rb

Overview

Genius::Errors::DynamicRescue module is used to call dynamically exceptions to each method in module or class, defined in Genius::Errors scope.

Class Method Summary collapse

Class Method Details

.rescue(klass) ⇒ Object

Genius::Errors::DynamicRescue.rescue -> value

Genius::Errors::DynamicRescue.rescue_from is a helper method, which, according to reflection, redefine singleton method for specified module, adding to it exception handler for DRY pattern.

@todo: add docs

Parameters:

  • klass (Object)

    Class name of structure - module/class/etc.

Returns:



134
135
136
137
138
139
# File 'lib/genius/api/errors.rb', line 134

def rescue(klass)
  DynamicRescue.rescue_from klass.singleton_methods, klass, GeniusExceptionSuperClass do |e|
    puts "Error description: #{e.msg}\nException type: #{e.exception_type}"
    # @todo make raise ExceptionKlass
  end
end

.rescue_from(meths, klass, exception, &handler) ⇒ Object

Parameters:

  • meths (Object)

    List of methods to redefine.

  • klass (Object)

    Class name of structure - module/class/etc.

  • exception (Object)

    Exception class.

  • handler (Proc)

    Body of rescue block.

Returns:



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/genius/api/errors.rb', line 146

def rescue_from(meths, klass, exception, &handler)
  meths.each do |meth|
    # store the previous implementation
    old = klass.singleton_method(meth)
    # wrap it
    klass.define_singleton_method(meth) do |*args|
      old.unbind.bind(klass).call(*args)
    rescue exception => e
      handler.call(e)
    end
  end
end