Skip to content

Commit

Permalink
feat: Add clearer message for CoercionNotSupportedError (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxveldink authored Jul 9, 2024
1 parent 11b7c17 commit 0eff2f7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/typed/coercion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ def self.register_coercer(coercer)

sig { type_parameters(:U).params(type: T::Types::Base, value: Value).returns(Result[Value, CoercionError]) }
def self.coerce(type:, value:)
coercer = CoercerRegistry.instance.select_coercer_by(type: type)
coercer = CoercerRegistry.instance.select_coercer_by(type:)

return Failure.new(CoercionNotSupportedError.new) unless coercer
return Failure.new(CoercionNotSupportedError.new(type:)) unless coercer

coercer.new.coerce(type: type, value: value)
coercer.new.coerce(type:, value:)
end
end
end
9 changes: 8 additions & 1 deletion lib/typed/coercion/coercion_not_supported_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

module Typed
module Coercion
class CoercionNotSupportedError < CoercionError; end
class CoercionNotSupportedError < CoercionError
extend T::Sig

sig { params(type: T::Types::Base).void }
def initialize(type:)
super("Coercer not found for type #{type}.")
end
end
end
end
11 changes: 11 additions & 0 deletions test/typed/coercion/coercion_not_supported_error_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# typed: true

require "test_helper"

class CoercionNotSupportedErrorTest < Minitest::Test
def test_formats_message
error = Typed::Coercion::CoercionNotSupportedError.new(type: T::Utils.coerce(RubyRank))

assert_equal("Coercer not found for type RubyRank.", error.message)
end
end
2 changes: 1 addition & 1 deletion test/typed/coercion_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ def test_when_coercer_isnt_matched_coerce_returns_failure
result = Typed::Coercion.coerce(type: T::Utils.coerce(BigDecimal), value: "testing")

assert_failure(result)
assert_error(Typed::Coercion::CoercionNotSupportedError.new, result)
assert_error(Typed::Coercion::CoercionNotSupportedError.new(type: T::Utils.coerce(BigDecimal)), result)
end
end

0 comments on commit 0eff2f7

Please sign in to comment.