Class: Midori::Sandbox

Inherits:
Object
  • Object
show all
Defined in:
lib/midori/sandbox.rb

Overview

Sandbox for global error capture

Class Method Summary collapse

Class Method Details

.add_rule(class_name, block) ⇒ nil

Add a rule to Sandbox

Parameters:

  • class_name (Class)

    the class to capture

  • block (Proc)

    what to do when captured

Returns:

  • (nil)

    nil



15
16
17
18
# File 'lib/midori/sandbox.rb', line 15

def add_rule(class_name, block)
  @handlers[class_name] = block
  nil
end

.capture(error) ⇒ nil

Detect what to run with given error

Parameters:

  • error (StandardError)

    the error captured

Returns:

  • (nil)

    nil



23
24
25
26
27
28
29
# 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) ⇒ nil

Run sandbox inside given clean room

Parameters:

Returns:

  • (nil)

    nil



35
36
37
38
39
40
41
# 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