From 0ea003bc7eaf3296e0313f3ddd3ddd7acbd4f801 Mon Sep 17 00:00:00 2001 From: Dai Okumura Date: Fri, 12 Apr 2024 15:27:44 +0900 Subject: [PATCH] Change error class from TypeError to TypeStruct::MultiTypeError on from_hash with other object than Hash --- lib/type_struct.rb | 6 +++++- lib/type_struct_test.rb | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/type_struct.rb b/lib/type_struct.rb index d77b15c..a8cd00c 100644 --- a/lib/type_struct.rb +++ b/lib/type_struct.rb @@ -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 = [] diff --git a/lib/type_struct_test.rb b/lib/type_struct_test.rb index 6acc253..64962f7 100644 --- a/lib/type_struct_test.rb +++ b/lib/type_struct_test.rb @@ -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