Sha256: 69f8b105a74df091edcb632e8e06c7a166f37ee959e59a73c880bed1843b6083

Contents?: true

Size: 985 Bytes

Versions: 1

Compression:

Stored size: 985 Bytes

Contents

# encoding: utf-8

module Rubymisc
  module Exceptional
    def ASSERT(&block)
      begin
        yield
      rescue Exception => e
        STDERR.puts e.inspect
        STDERR.puts e.backtrace
        # raise e.class, "#{e.message}", caller
      end
    end

    ##
    # @example
    #   begin; raise 'Timeout socket'; rescue errors_with_message(/socket/); p 'socket E'; end
    #
    def errors_with_message(pattern)
      m = Module.new
      (class << m; self; end).instance_eval do
        define_method(:===) do |e|
          pattern === e.message
        end
      end
      m
    end

    ##
    # @example
    #   rescue => e; raise NestedException.new('Error B', e); end
    #   rescue; raise NestedException, 'Error B'; end
    #
    class NestedException < StandardError
      attr_reader :original
      def initialize(msg, original = $!)
        super(msg)
        @original = original
      end
    end
  end
end

::Object.module_eval 'include Rubymisc::Exceptional'

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubymisc-0.0.3 lib/rubymisc/exceptional.rb