Sha256: 857dd9dd25c4a016d2954163a7b9edd8945ec977d2b8fcc2df2b91e81ebf2f35

Contents?: true

Size: 1.32 KB

Versions: 10

Compression:

Stored size: 1.32 KB

Contents

##
# Sandbox for global error capture
class Midori::Sandbox
  class << self
    def class_initialize
      @handlers = Hash.new
      @handlers[Midori::Exception::InternalError] = proc {|e| Midori::Response.new(500, {}, "#{e.inspect} #{e.backtrace}")}
      @handlers[Midori::Exception::NotFound] = proc {|_e| Midori::Response.new(404, {}, '404 Not Found')}
    end

    # Add a rule to Sandbox
    # @param [Class] class_name the class to capture
    # @param [Proc] block what to do when captured
    # @return [nil] nil
    def add_rule(class_name, block)
      @handlers[class_name] = block
      nil
    end

    # Detect what to run with given error
    # @param [StandardError] error the error captured
    # @return [nil] nil
    def capture(error)
      if @handlers[error.class].nil?
        @handlers[Midori::Exception::InternalError].call(error)
      else
        @handlers[error.class].call(error) 
      end
    end

    # Run sandbox inside given clean room
    # @param [Midori::CleanRoom] clean_room Clean room to run
    # @param [Proc] function the block to run
    # @return [nil] nil
    def run(clean_room, function, *args)
      begin
        function.to_lambda(clean_room).call(*args)
      rescue StandardError => e
        capture(e)
      end
    end
  end

  private_class_method :class_initialize
  class_initialize
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
em-midori-0.2.4 lib/midori/sandbox.rb
em-midori-0.2.3 lib/midori/sandbox.rb
em-midori-0.2.2 lib/midori/sandbox.rb
em-midori-0.2.1 lib/midori/sandbox.rb
em-midori-0.2.0 lib/midori/sandbox.rb
em-midori-0.1.12 lib/midori/sandbox.rb
em-midori-0.1.11 lib/midori/sandbox.rb
em-midori-0.1.10 lib/midori/sandbox.rb
em-midori-0.1.9 lib/midori/sandbox.rb
em-midori-0.1.8 lib/midori/sandbox.rb