Sha256: 2261711c47ebd4537abf90316b8f83df0403b90df00096cc4bca48def3636c92

Contents?: true

Size: 1.39 KB

Versions: 24

Compression:

Stored size: 1.39 KB

Contents

module Ably::Modules
  # SafeYield provides the method safe_yield that will yield to the consumer
  # who provided a block, however any exceptions will be caught, logged, and
  # operation of the client library will continue.
  #
  # An exception in a callback provided by a developer should not break this client library
  # and stop further execution of code.
  #
  # @note this Module requires that the method #logger is available
  #
  # @api private
  module SafeYield
    private

    def safe_yield(block, *args)
      block.call(*args)
    rescue StandardError => e
      message = "An exception in an external block was caught. #{e.class}: #{e.message}\n#{e.backtrace.join("\n")}"
      safe_yield_log_error message
    end

    def safe_yield_log_error(message)
      if defined?(:logger) && logger.respond_to?(:error)
        return logger.error message
      end
    rescue StandardError
      fallback_logger.error message
    end

    def fallback_logger
      @fallback_logger ||= ::Logger.new(STDOUT).tap do |logger|
        logger.formatter = proc do |severity, datetime, progname, msg|
          [
            "#{datetime.strftime("%Y-%m-%d %H:%M:%S.%L")} #{::Logger::SEV_LABEL[severity]} #{msg}",
            "Warning: SafeYield expects the method #logger to be defined in the class it is included in, the method was not found in #{self.class}"
          ].join("\n")
        end
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 2 rubygems

Version Path
ably-rest-1.0.5 lib/submodules/ably-ruby/lib/ably/modules/safe_yield.rb
ably-1.0.5 lib/ably/modules/safe_yield.rb
ably-1.0.4 lib/ably/modules/safe_yield.rb
ably-1.0.3 lib/ably/modules/safe_yield.rb
ably-1.0.2 lib/ably/modules/safe_yield.rb
ably-1.0.1 lib/ably/modules/safe_yield.rb
ably-rest-1.0.0 lib/submodules/ably-ruby/lib/ably/modules/safe_yield.rb
ably-1.0.0 lib/ably/modules/safe_yield.rb
ably-rest-0.8.15 lib/submodules/ably-ruby/lib/ably/modules/safe_yield.rb
ably-0.8.15 lib/ably/modules/safe_yield.rb
ably-rest-0.9.3 lib/submodules/ably-ruby/lib/ably/modules/safe_yield.rb
ably-rest-0.9.2 lib/submodules/ably-ruby/lib/ably/modules/safe_yield.rb
ably-rest-0.9.1 lib/submodules/ably-ruby/lib/ably/modules/safe_yield.rb
ably-rest-0.9.0 lib/submodules/ably-ruby/lib/ably/modules/safe_yield.rb
ably-rest-0.8.14 lib/submodules/ably-ruby/lib/ably/modules/safe_yield.rb
ably-0.8.14 lib/ably/modules/safe_yield.rb
ably-rest-0.8.13 lib/submodules/ably-ruby/lib/ably/modules/safe_yield.rb
ably-0.8.13 lib/ably/modules/safe_yield.rb
ably-0.8.12 lib/ably/modules/safe_yield.rb
ably-0.8.11 lib/ably/modules/safe_yield.rb