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 3, 2023
1 parent 16448b1 commit 5c4efc2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
43 changes: 24 additions & 19 deletions contrib/ruby/lib/trilogy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ module ConnectionError

class BaseError < StandardError
include Error

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

class BaseConnectionError < BaseError
include ConnectionError
end

# Trilogy::ClientError is the base error type for invalid queries or parameters
Expand All @@ -36,19 +47,19 @@ class TimeoutError < Errno::ETIMEDOUT
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
Expand All @@ -58,12 +69,6 @@ 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 5c4efc2

Please sign in to comment.