Skip to content

Commit

Permalink
Merge pull request #16 from okumud/change-multitype-error-for-from_ha…
Browse files Browse the repository at this point in the history
…sh_with_other_object

Change error class from `TypeError` to `TypeStruct::MultiTypeError` on `from_hash` with other object than Hash
  • Loading branch information
ksss authored Apr 14, 2024
2 parents 0a3810a + 0ea003b commit 7f2ce3c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 5 additions & 1 deletion lib/type_struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ def to_h
module ClassMethods
def from_hash(arg)
unless h = Hash.try_convert(arg)
raise TypeError, "no implicit conversion from #{arg} to Hash"
begin
raise TypeError, "no implicit conversion from #{arg} to Hash"
rescue => e
raise TypeStruct::MultiTypeError, [e]
end
end
args = {}
errors = []
Expand Down
4 changes: 2 additions & 2 deletions lib/type_struct_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,11 @@ def test_s_from_hash_with_other_object(t)
o = Object.new
begin
a.from_hash(o)
rescue TypeError => e
rescue TypeStruct::MultiTypeError => e
rescue => e
t.error("Unexpected error #{e.class}")
else
t.error("should raise TypeError")
t.error("should raise MultiTypeError")
end

def o.to_hash
Expand Down

0 comments on commit 7f2ce3c

Please sign in to comment.