Skip to content

Commit

Permalink
Fix ProtocolError => ConnectionError and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianna-chang-shopify committed Jan 6, 2023
1 parent 16448b1 commit f2ad5bd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
55 changes: 32 additions & 23 deletions contrib/ruby/lib/trilogy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ module ConnectionError

class BaseError < StandardError
include Error

attr_reader :error_code

def initialize(error_message = nil, error_code = nil)
message = error_code ? "#{error_code}: #{error_message}" : error_message
super(message)
@error_code = error_code
end
end

class BaseConnectionError < BaseError
include ConnectionError
end

# Trilogy::ClientError is the base error type for invalid queries or parameters
Expand All @@ -28,42 +40,39 @@ class QueryError < ClientError
class CastError < ClientError
end


class TimeoutError < Errno::ETIMEDOUT
include ConnectionError

attr_reader :error_code

def initialize(error_message = nil, error_code = nil)
super
@error_code = error_code
end
end

class ProtocolError < BaseError
ERROR_CODES = {
1205 => TimeoutError, # ER_LOCK_WAIT_TIMEOUT
1044 => ConnectionError, # ER_DBACCESS_DENIED_ERROR
1045 => ConnectionError, # ER_ACCESS_DENIED_ERROR
1152 => ConnectionError, # ER_ABORTING_CONNECTION
1153 => ConnectionError, # ER_NET_PACKET_TOO_LARGE
1154 => ConnectionError, # ER_NET_READ_ERROR_FROM_PIPE
1155 => ConnectionError, # ER_NET_FCNTL_ERROR
1156 => ConnectionError, # ER_NET_PACKETS_OUT_OF_ORDER
1157 => ConnectionError, # ER_NET_UNCOMPRESS_ERROR
1158 => ConnectionError, # ER_NET_READ_ERROR
1159 => ConnectionError, # ER_NET_READ_INTERRUPTED
1160 => ConnectionError, # ER_NET_ERROR_ON_WRITE
1161 => ConnectionError, # ER_NET_WRITE_INTERRUPTED
1927 => ConnectionError, # ER_CONNECTION_KILLED
1044 => BaseConnectionError, # ER_DBACCESS_DENIED_ERROR
1045 => BaseConnectionError, # ER_ACCESS_DENIED_ERROR
1152 => BaseConnectionError, # ER_ABORTING_CONNECTION
1153 => BaseConnectionError, # ER_NET_PACKET_TOO_LARGE
1154 => BaseConnectionError, # ER_NET_READ_ERROR_FROM_PIPE
1155 => BaseConnectionError, # ER_NET_FCNTL_ERROR
1156 => BaseConnectionError, # ER_NET_PACKETS_OUT_OF_ORDER
1157 => BaseConnectionError, # ER_NET_UNCOMPRESS_ERROR
1158 => BaseConnectionError, # ER_NET_READ_ERROR
1159 => BaseConnectionError, # ER_NET_READ_INTERRUPTED
1160 => BaseConnectionError, # ER_NET_ERROR_ON_WRITE
1161 => BaseConnectionError, # ER_NET_WRITE_INTERRUPTED
1927 => BaseConnectionError, # ER_CONNECTION_KILLED
}

attr_reader :error_code, :error_message

class << self
def from_code(message, code)
ERROR_CODES.fetch(code, self).new(message, code)
end
end

def initialize(error_message, error_code)
super("#{error_code}: #{error_message}")
@error_code = error_code
@error_message = error_message
end
end

class SSLError < BaseError
Expand Down
8 changes: 8 additions & 0 deletions contrib/ruby/test/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,14 @@ def test_timeout_error
end
end

def test_connection_error
err = assert_raises Trilogy::BaseConnectionError do
new_tcp_client(username: "foo")
end

assert_includes err.message, "Access denied for user 'foo'"
end

def test_database_error
client = new_tcp_client

Expand Down

0 comments on commit f2ad5bd

Please sign in to comment.