class Racket::Utils::Exceptions::ExceptionHandler

Handles exceptions dynamically

Public Class Methods

run_block(errors) { || ... } click to toggle source

Runs a block. If no exceptions are raised, this method returns true. If any of the provided error types are raised, this method returns false. If any other exception is raised, this method will just forward the exception.

@param [Array] errors @return [true|flase]

# File lib/racket/utils/exceptions.rb, line 33
def self.run_block(errors)
  raise 'Need a block' unless block_given?
  begin
    true.tap { yield }
  rescue boolean_module(errors)
    false
  end
end

Private Class Methods

boolean_module(errors) click to toggle source

Returns an anonymous module that can be used to rescue exceptions dynamically.

# File lib/racket/utils/exceptions.rb, line 43
def self.boolean_module(errors)
  Module.new do
    singleton_class.instance_eval do
      define_method(:===) do |error|
        errors.any? { |err| error.class <= err }
      end
    end
  end
end