Skip to content

Commit

Permalink
Replace === with regular === on SyscallError
Browse files Browse the repository at this point in the history
Without this Trilogy::SyscallError::* matches all matching instances of
::Errno::*, which makes the subclass useless in differentiating.

Co-authored-by: Daniel Colson <composerinteralia@github.com>
  • Loading branch information
jhawthorn and composerinteralia committed Dec 18, 2023
1 parent f31c5da commit a29831f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 5 additions & 2 deletions contrib/ruby/lib/trilogy/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ module ConnectionError
end

# Trilogy may raise various syscall errors, which we treat as Trilogy::Errors.
class SyscallError
module SyscallError
ERRORS = {}

Errno.constants
.map { |c| Errno.const_get(c) }.uniq
.select { |c| c.is_a?(Class) && c < SystemCallError }
.each do |c|
errno_name = c.to_s.split('::').last
ERRORS[c::Errno] = const_set(errno_name, Class.new(c) { include Trilogy::ConnectionError })
ERRORS[c::Errno] = const_set(errno_name, Class.new(c) {
include Trilogy::ConnectionError
singleton_class.define_method(:===, Module.instance_method(:===))
})
end

ERRORS.freeze
Expand Down
10 changes: 10 additions & 0 deletions contrib/ruby/test/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1099,4 +1099,14 @@ def test_connection_options_casting

assert client.query("SELECT 1")
end

def test_error_classes_exclusively_match_subclasses
klass = Trilogy::SyscallError::ECONNRESET
assert_operator klass, :===, klass.new
refute_operator klass, :===, Errno::ECONNRESET.new

assert_operator Errno::ECONNRESET, :===, klass.new
assert_operator SystemCallError, :===, klass.new
assert_operator Trilogy::ConnectionError, :===, klass.new
end
end

0 comments on commit a29831f

Please sign in to comment.