Module: Enhanced::ExceptionContext

Extended by:
ExceptionContext
Included in:
ExceptionContext
Defined in:
lib/enhanced/exception_context.rb

Constant Summary collapse

REGISTRY =
{}
MUTEX =
Monitor.new

Instance Method Summary collapse

Instance Method Details

#clear_allObject



43
44
45
46
47
# File 'lib/enhanced/exception_context.rb', line 43

def clear_all
  MUTEX.synchronize do
    REGISTRY.clear
  end
end

#clear_context(exception) ⇒ Object



37
38
39
40
41
# File 'lib/enhanced/exception_context.rb', line 37

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

#context_for(exception) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/enhanced/exception_context.rb', line 21

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

#store_context(exception, context) ⇒ Object



15
16
17
18
19
# File 'lib/enhanced/exception_context.rb', line 15

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