def coerce(input)
type.coerce(input)
rescue Avromatic::Model::UnknownAttributeError => e
raise Avromatic::Model::CoercionError.new(
"Value for #{owner.name}##{name} could not be coerced to a #{type.name} " \
"because the following unexpected attributes were provided: #{e.unknown_attributes.join(', ')}. " \
"Only the following attributes are allowed: #{e.allowed_attributes.join(', ')}. " \
"Provided argument: #{input.inspect}"
)
rescue StandardError
if type.input_classes && type.input_classes.none? { |input_class| input.is_a?(input_class) }
raise Avromatic::Model::CoercionError.new(
"Value for #{owner.name}##{name} could not be coerced to a #{type.name} " \
"because a #{input.class.name} was provided but expected a #{type.input_classes.map(&:name).to_sentence(
two_words_connector: ' or ', last_word_connector: ', or '
)}. " \
"Provided argument: #{input.inspect}"
)
elsif input.is_a?(Hash) && type.is_a?(Avromatic::Model::Types::UnionType)
raise Avromatic::Model::CoercionError.new(
"Value for #{owner.name}##{name} could not be coerced to a #{type.name} " \
"because no union member type matches the provided attributes: #{input.inspect}"
)
else
raise Avromatic::Model::CoercionError.new(
"Value for #{owner.name}##{name} could not be coerced to a #{type.name}. " \
"Provided argument: #{input.inspect}"
)
end
end