Sha256: ba4e6eab6366d51d663bfe6788aed9b789ec9bf3341171c34c6597196f3e3b6a

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

require "errbase"

module Safely
  VERSION = "0.1.0"

  class << self
    attr_accessor :env, :raise_envs, :tag, :report_exception_method

    def report_exception(e)
      report_exception_method.call(e)
    end
  end

  DEFAULT_EXCEPTION_METHOD = proc do |e|
    e = e.dup # leave original exception unmodified
    e.message.prepend("[safely] ") if e.message && Safely.tag
    Errbase.report(e)
  end

  self.env = ENV["RACK_ENV"] || ENV["RAILS_ENV"] || "development"
  self.tag = true
  self.report_exception_method = DEFAULT_EXCEPTION_METHOD
  self.raise_envs = %w(development test)

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

Object.send :include, Safely::Methods

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
safely_block-0.1.0 lib/safely_block.rb