Module: Miteru::Concerns::ErrorUnwrappable

Extended by:
ActiveSupport::Concern
Defined in:
lib/miteru/concerns/error_unwrappable.rb

Overview

Error unwrappable concern

Instance Method Summary collapse

Instance Method Details

#unwrap_error(err) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/miteru/concerns/error_unwrappable.rb', line 11

def unwrap_error(err)
  return err unless err.is_a?(Dry::Monads::UnwrapError)

  # NOTE: UnwrapError's receiver can be either of:
  #       - Dry::Monads::Try::Error
  #       - Dry::Monads::Result::Failure
  receiver = err.receiver
  case receiver
  when Dry::Monads::Try::Error
    # Error may be wrapped like Matryoshka
    unwrap_error receiver.exception
  when Dry::Monads::Failure
    unwrap_error receiver.failure
  else
    err
  end
end