Sha256: bd2df7b436bcd33268160a7a3b90342a0b85a4925967125171c9b5fa1b1fd696

Contents?: true

Size: 926 Bytes

Versions: 5

Compression:

Stored size: 926 Bytes

Contents

require 'weakref'

require_relative 'context'

require 'weakref'
require 'monitor'

module EnhancedExceptionContext
  extend self

  REGISTRY = {}
  MUTEX = Monitor.new

  def store_context(exception, context)
    MUTEX.synchronize do
      REGISTRY[exception.object_id] = { weak_exc: WeakRef.new(exception), context: context }
    end
  end

  def context_for(exception)
    MUTEX.synchronize do
      entry = REGISTRY[exception.object_id]
      return nil unless entry

      begin
        _ = entry[:weak_exc].__getobj__ # ensure exception is still alive
        entry[:context]
      rescue RefError
        # Exception no longer alive, clean up
        REGISTRY.delete(exception.object_id)
        nil
      end
    end
  end

  def clear_context(exception)
    MUTEX.synchronize do
      REGISTRY.delete(exception.object_id)
    end
  end

  def clear_all
    MUTEX.synchronize do
      REGISTRY.clear
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
enhanced_errors-3.0.3 lib/enhanced/enhanced_exception_context.rb
enhanced_errors-3.0.2 lib/enhanced/enhanced_exception_context.rb
enhanced_errors-3.0.1 lib/enhanced/enhanced_exception_context.rb
enhanced_errors-3.0.0 lib/enhanced/enhanced_exception_context.rb
enhanced_errors-2.2.0 lib/enhanced/enhanced_exception_context.rb