Sha256: 4fdb6d3bf768657830c314e217d486a0d2cb2cd6e69830cf0f5fe2aed4fbd5cd

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

class BCDD::Result::Error < StandardError
  def self.build(**_kargs)
    new
  end

  class NotImplemented < self
  end

  class MissingTypeArgument < self
    def initialize(_arg = nil)
      super('A type (argument) is required to invoke the #on/#on_type method')
    end
  end

  class UnexpectedOutcome < self
    def self.build(outcome:, origin:)
      message =
        "Unexpected outcome: #{outcome.inspect}. The #{origin} must return this object wrapped by " \
        'BCDD::Result::Success or BCDD::Result::Failure'

      new(message)
    end
  end

  class WrongResultSubject < self
    def self.build(given_result:, expected_subject:)
      message =
        "You cannot call #and_then and return a result that does not belong to the subject!\n" \
        "Expected subject: #{expected_subject.inspect}\n" \
        "Given subject: #{given_result.send(:subject).inspect}\n" \
        "Given result: #{given_result.inspect}"

      new(message)
    end
  end

  class WrongSubjectMethodArity < self
    def self.build(subject:, method:)
      new("#{subject.class}##{method.name} has unsupported arity (#{method.arity}). Expected 0 or 1.")
    end
  end

  class UnhandledTypes < self
    def self.build(types:)
      subject = types.size == 1 ? 'This was' : 'These were'

      new("You must handle all cases. #{subject} not handled: #{types.map(&:inspect).join(', ')}")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bcdd-result-0.5.0 lib/bcdd/result/error.rb