Skip to content

Commit

Permalink
Add Custom error class for TypeError
Browse files Browse the repository at this point in the history
  • Loading branch information
okumud committed Dec 1, 2023
1 parent 0a3810a commit 5c663ae
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ p sample.str
#=> "instance of String"

sample.string #=> NoMethodError
sample.str = 1 #=> TypeError
sample.str = 1 #=> TypeStruct::TypeError
```

### Recursive Mapping
Expand Down Expand Up @@ -184,8 +184,8 @@ Baz = TypeStruct.new(
)
p Baz.new(qux: { "a" => [1, 2, 3] }) #=> #<Baz qux={"a"=>[1, 2, 3]}>
p Baz.from_hash(qux: { "a" => [1, 2, 3] }) #<Baz qux={"a"=>[1, 2, 3]}>
p Baz.new(qux: { :a => [1, 2, 3] }) #=> TypeError
p Baz.new(qux: { "a" => [1, 2, nil] }) #=> TypeError
p Baz.new(qux: { :a => [1, 2, 3] }) #=> TypeStruct::TypeError
p Baz.new(qux: { "a" => [1, 2, nil] }) #=> TypeStruct::TypeError
```

### Interface
Expand All @@ -200,7 +200,7 @@ Foo = TypeStruct.new(
# or Interface.new(:read, :write) on required 'type_struct/ext'
)
Foo.new(bar: $stdin)
Foo.new(bar: 1) #=> TypeError
Foo.new(bar: 1) #=> TypeStruct::TypeError
```

### Mix
Expand All @@ -214,7 +214,7 @@ Baz = TypeStruct.new(
p Baz.new(qux: [1]) #=> #<AAA::Baz qux=[1]>
p Baz.new(qux: [true, false]) #=> #<AAA::Baz qux=[true, false]>
p Baz.new(qux: nil) #=> #<AAA::Baz qux=nil>
p Baz.new(qux: 1) #=> TypeError
p Baz.new(qux: 1) #=> TypeStruct::TypeError
p Baz.from_hash(qux: [1, 2, false, true]) #=> #<A::Baz qux=[1, 2, false, true]>
```

Expand Down
1 change: 1 addition & 0 deletions lib/type_struct/exceptions.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class TypeStruct
UnionNotFoundError = Class.new(StandardError)
TypeError = Class.new(::TypeError)

class MultiTypeError < StandardError
THIS_LIB_REGEXP = %r{lib/type_struct[./]}
Expand Down
11 changes: 7 additions & 4 deletions lib/type_struct_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,8 @@ def test_s_from_hash_with_array_of(t)
a.from_hash(a: 1, b: 'a')
rescue TypeStruct::MultiTypeError => e
[
%r{lib/type_struct_test.rb:#{line}:in TypeError.*?#a expect ArrayOf\(Integer\) got 1}o,
%r{lib/type_struct_test.rb:#{line}:in TypeError.*?#b expect Integer got "a"}o,
%r{lib/type_struct_test.rb:#{line}:in TypeStruct::TypeError.*?#a expect ArrayOf\(Integer\) got 1}o,
%r{lib/type_struct_test.rb:#{line}:in TypeStruct::TypeError.*?#b expect Integer got "a"}o,
].each do |expect|
unless expect =~ e.message
t.error("message was changed: #{e.message}")
Expand Down Expand Up @@ -503,9 +503,12 @@ def test_initialize_with_to_hash(t)
o = Object.new
begin
t.new(o)
rescue TypeError
rescue TypeStruct::TypeError => e
unless e.is_a?(::TypeError)
t.error("expect TypeStruct::TypeError is inherit of TypeError")
end
else
t.error("expect TypeError")
t.error("expect TypeStruct::TypeError")
end
def o.to_hash
{foo: "a"}
Expand Down

0 comments on commit 5c663ae

Please sign in to comment.