class Midori::Sandbox

Sandbox for global error capture

Public Class Methods

add_rule(class_name, block) click to toggle source

Add a rule to Sandbox @param [Class] class_name the class to capture @param [Proc] block what to do when captured @return [nil] nil

# File lib/midori/sandbox.rb, line 15
def add_rule(class_name, block)
  @handlers[class_name] = block
  nil
end
capture(error) click to toggle source

Detect what to run with given error @param [StandardError] error the error captured @return [nil] nil

# File lib/midori/sandbox.rb, line 23
def capture(error)
  if @handlers[error.class].nil?
    @handlers[Midori::Exception::InternalError].call(error)
  else
    @handlers[error.class].call(error) 
  end
end
run(clean_room, function, *args) click to toggle source

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

# File lib/midori/sandbox.rb, line 35
def run(clean_room, function, *args)
  begin
    function.to_lambda(clean_room).call(*args)
  rescue StandardError => e
    capture(e)
  end
end

Private Class Methods

class_initialize() click to toggle source
# File lib/midori/sandbox.rb, line 5
def class_initialize
  @handlers = Hash.new
  @handlers[Midori::Exception::InternalError] = proc {|e| Midori::Response.new(status: 500, body: "#{e.inspect} #{e.backtrace}")}
  @handlers[Midori::Exception::NotFound] = proc {|_e| Midori::Response.new(status: 404, body: '404 Not Found')}
end