Sha256: 0afda3c8c4abe98390051980794e54830fa9a1703f6c95f464846d274494d2da

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

module Fear
  class Failure
    include Try
    include Dry::Equalizer(:exception)
    include RightBiased::Left
    include FailurePatternMatch.mixin

    # @param [StandardError]
    def initialize(exception)
      @exception = exception
    end

    attr_reader :exception

    # @return [Boolean]
    def success?
      false
    end

    # @return [true]
    def failure?
      true
    end

    # @raise
    def get
      raise exception
    end

    # @return [Try] of calling block
    def or_else(*args)
      super
    rescue StandardError => error
      Failure.new(error)
    end

    # @return [Failure] self
    def flatten
      self
    end

    # @return [Failure] self
    def select
      self
    end

    # @yieldparam [Exception]
    # @yieldreturn [Try]
    # @return [Try]
    def recover_with
      yield(exception).tap do |result|
        Utils.assert_type!(result, Success, Failure)
      end
    rescue StandardError => error
      Failure.new(error)
    end

    # @yieldparam [Exception]
    # @yieldreturn [any]
    # @return [Try]
    def recover
      Success.new(yield(exception))
    rescue StandardError => error
      Failure.new(error)
    end

    # @return [Left]
    def to_either
      Left.new(exception)
    end

    # Used in case statement
    # @param other [any]
    # @return [Boolean]
    def ===(other)
      if other.is_a?(Failure)
        exception === other.exception
      else
        super
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fear-0.11.0 lib/fear/failure.rb