Sha256: ccc23db5d2d457a897ce29f9a2681f93a091b2fb258e506625e927d4da4a7cf0

Contents?: true

Size: 892 Bytes

Versions: 62

Compression:

Stored size: 892 Bytes

Contents

# require "eitil_core/safe_executions/safe_rescue"

Kernel.module_eval do

  # WORK IN PROCESS
  # => accept a second block, prefarabbly not as proc, with code to be exceuted in case of rescue
  # => or would it be possible to patch the 'rescue' keyword itself, so that we don't have to accept
  # blocks as arguments?

  def safe_rescue(error, &blk)

    unless blk
      raise ArgumentError, "a block should be passed"
    end

    unless error.is_a?(Class) or error.is_a?(String)
      raise ArgumentError, "the given 'error' should either be a constant or string"
    end

    if error.is_a?(Class)
      error_const = error

    elsif error.is_a?(String) and Object.const_defined?(error)
      error_const = error.constantize

    elsif error.is_a?(String)
      error_const = Object.const_set(error, Class.new(StandardError))

    end

    blk.call  

    rescue error_const
  end

end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
eitil-2.0.8 eitil_core/lib/eitil_core/safe_executions/safe_rescue.rb
eitil-2.0.7 eitil_core/lib/eitil_core/safe_executions/safe_rescue.rb
eitil-2.0.6 eitil_core/lib/eitil_core/safe_executions/safe_rescue.rb
eitil-2.0.5 eitil_core/lib/eitil_core/safe_executions/safe_rescue.rb
eitil-2.0.4 eitil_core/lib/eitil_core/safe_executions/safe_rescue.rb
eitil-2.0.3 eitil_core/lib/eitil_core/safe_executions/safe_rescue.rb
eitil-2.0.2 eitil_core/lib/eitil_core/safe_executions/safe_rescue.rb
eitil-2.0.1 eitil_core/lib/eitil_core/safe_executions/safe_rescue.rb
eitil-2.0.0 eitil_core/lib/eitil_core/safe_executions/safe_rescue.rb
eitil-1.4.1 eitil_core/lib/eitil_core/safe_executions/safe_rescue.rb
eitil-1.4.0 eitil_core/lib/eitil_core/safe_executions/safe_rescue.rb
eitil-1.3.12 eitil_core/lib/eitil_core/safe_executions/safe_rescue.rb
eitil-1.3.11 eitil_core/lib/eitil_core/safe_executions/safe_rescue.rb
eitil-1.3.10 eitil_core/lib/eitil_core/safe_executions/safe_rescue.rb
eitil-1.3.9 eitil_core/lib/eitil_core/safe_executions/safe_rescue.rb
eitil-1.3.8 eitil_core/lib/eitil_core/safe_executions/safe_rescue.rb
eitil-1.3.7 eitil_core/lib/eitil_core/safe_executions/safe_rescue.rb
eitil-1.3.6 eitil_core/lib/eitil_core/safe_executions/safe_rescue.rb
eitil-1.3.5 eitil_core/lib/eitil_core/safe_executions/safe_rescue.rb
eitil-1.3.4 eitil_core/lib/eitil_core/safe_executions/safe_rescue.rb