Skip to content

Commit

Permalink
Fix error transformation on older Rubies
Browse files Browse the repository at this point in the history
  • Loading branch information
jhawthorn committed Jan 31, 2025
1 parent 3adf930 commit 3fcd720
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions contrib/ruby/lib/trilogy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
require "trilogy/encoding"

class Trilogy
IO_TIMEOUT_ERROR =
if defined?(IO::TimeoutError)
IO::TimeoutError
else
Class.new
end
private_constant :IO_TIMEOUT_ERROR

def initialize(options = {})
options[:port] = options[:port].to_i if options[:port]
mysql_encoding = options[:encoding] || "utf8mb4"
Expand Down Expand Up @@ -47,9 +55,9 @@ def initialize(options = {})
path = options[:socket] ||= "/tmp/mysql.sock"
socket = UNIXSocket.new(path)
end
rescue IO::TimeoutError => e
rescue Errno::ETIMEDOUT, IO_TIMEOUT_ERROR => e
raise Trilogy::TimeoutError, e.message
rescue Socket::ResolutionError => e
rescue SocketError => e
connection_str = host ? "#{host}:#{port}" : path
raise Trilogy::BaseConnectionError, "unable to connect to \"#{connection_str}\": #{e.message}"
rescue => e
Expand Down

0 comments on commit 3fcd720

Please sign in to comment.