dry-rb / dry-container

A simple, configurable object container implemented in Ruby
https://dry-rb.org/gems/dry-container
MIT License
335 stars 41 forks source link

Wrap FrozenError, to state that the container itself is frozen #76

Closed cllns closed 2 years ago

cllns commented 3 years ago

Before:

FrozenError: 
  can't modify frozen Concurrent::Hash: {"foo"=>#<Dry::Container::Item::Callable:0x00007f9a9f2eaea0 @item="bar", @options={:call=>false}>}

After:

FrozenError:
  can't modify frozen Dry::Container (when attempting to register 'baz')

That a Dry::Container is implemented with Concurrent::Hash is an implemention detail. I think wrapping it helps users of the library see that this is an expected exception, rather than an error within the library. And the text in the parentheses should help users when they encounter this error.

I also split up the specs and removed a conditional for whether Ruby is 2.5+, since this gem only supports 2.5+ now.

(Relevant commit where .freeze was implemented: https://github.com/dry-rb/dry-container/commit/6fc5cd2650b1888ac6f4fc56592878695d296979)

cllns commented 3 years ago

Another fun enhancement to this would be to store a @_freeze_caller = caller when freezing it, then surface it when this error occurs, telling users where they froze the container.

cllns commented 2 years ago

Rebased on master to resolve conflicts