class Object
  # Always throw errors in development and test, they're too easy to miss in the logs,
  # and we should be "safely" handling these things, not swallowing them
  # In production, swallowing errors makes for a much richer experience
  def notify(error)
    raise error unless Rails.env.production?
    Airbrake.notify(error)
  end

  # If a 3rd-party service is down, like Stripe or AWS, 
  # we might want to just retry it again after a bit of a wait
  #
  # Note rack-timeout is set to 1 second, but hopefully we're only ever 
  # talking to 3rd-party APIs in background jobs, eh? ;-)
  def retriable(*errors, &block)
    options  = errors.extract_options! # rails-ism: if you're using this elsewhere, adjust accordingly
    attempts = 0 # how many times have we tried this already?
    max      = options.delete(:max) || 0

    begin    
      yield
    rescue *errors
      Airbrake.notify $!

      attempts += 1
      attempts > max         and raise
      sleep 1.15 ** attempts and retry
    end
  end
end