Module: Safely::Methods

Included in:
Safely
Defined in:
lib/safely/core.rb

Instance Method Summary collapse

Instance Method Details

#safely(tag: nil, sample: nil, except: nil, only: nil, silence: nil, throttle: false, default: nil, context: {}) ⇒ Object Also known as: yolo



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/safely/core.rb', line 49

def safely(tag: nil, sample: nil, except: nil, only: nil, silence: nil, throttle: false, default: nil, context: {})
  yield
rescue *Array(only || StandardError) => e
  raise e if Array(except).any? { |c| e.is_a?(c) }
  raise e if Safely.raise_envs.include?(Safely.env)
  if sample ? rand < 1.0 / sample : true
    begin
      unless Array(silence).any? { |c| e.is_a?(c) } || Safely.throttled?(e, throttle)
        Safely.report_exception(e, tag: tag, context: context)
      end
    rescue => e2
      $stderr.puts "FAIL-SAFE #{e2.class.name}: #{e2.message}"
    end
  end
  default
end