lib/eco/api/usecases/base_io.rb in eco-helpers-2.0.21 vs lib/eco/api/usecases/base_io.rb in eco-helpers-2.0.22

- old
+ new

@@ -3,10 +3,23 @@ class UseCases # Basic class to manage InputOuput for usecases class BaseIO < BaseCase @types = BaseCase.types + class MissingParameter < StandardError + attr_reader :type, :required, :given + + def initialize(msg = nil, type: nil, required:, given:) + @type = type + @required = required + @given = given + msg += " of type '#{type}'" if type + msg += " requires an object '#{required}'. Given: #{given}." + super(msg) + end + end + class << self def input_required?(type) !valid_type?(type) || [:import, :sync].include?(type) end @@ -78,16 +91,16 @@ private def validate_args(input:, people:, session:, options:) case when !session.is_a?(Eco::API::Session) - raise "A UseCase needs a Session object. Given: #{session}" + raise MissingParameter.new("UseCase", required: :session, given: session.class) when input_required? && !input - raise "UseCase of type '#{type}' requires a valid input. None given" + raise MissingParameter.new("UseCase", type: type, required: :input, given: input.class) when people_required? && !people.is_a?(Eco::API::Organization::People) - raise "UseCase of type '#{type}' requires a People object. Given: #{people}" + raise MissingParameter.new("UseCase", type: type, required: :people, given: people.class) when !options || (options && !options.is_a?(Hash)) - raise "To inject dependencies via ':options' it should be a Hash object. Given: #{options}" + raise MissingParameter.new("Use Case options", required: :Hash, given: options.class) end true end def input_required?