Skip to content

Commit

Permalink
Handle query errors
Browse files Browse the repository at this point in the history
  • Loading branch information
composerinteralia committed Apr 12, 2024
1 parent a275e0d commit 88262cb
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
1 change: 1 addition & 0 deletions lib/nocturne.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "socket"
require "digest"
require_relative "nocturne/error"
require_relative "nocturne/protocol"
require_relative "nocturne/protocol/connection"
require_relative "nocturne/protocol/query"
require_relative "nocturne/read/packet"
Expand Down
3 changes: 3 additions & 0 deletions lib/nocturne/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ class Error < StandardError

class ConnectionError < Error
end

class QueryError < Error
end
end
14 changes: 14 additions & 0 deletions lib/nocturne/protocol.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#frozen_string_literal: true

class Nocturne
module Protocol
def self.read_error(packet)
packet.int
code = packet.int(2)
packet.strn(1)
packet.strn(5)
message = packet.eof_str
[code, message]
end
end
end
13 changes: 2 additions & 11 deletions lib/nocturne/protocol/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def establish
if packet.ok?
return
elsif packet.err?
code, message = read_error(packet)
code, message = Protocol.read_error(packet)
raise ConnectionError, "#{code}: #{message}"
elsif packet.int == 0xFE # auth switch
plugin = packet.nulstr
Expand Down Expand Up @@ -85,7 +85,7 @@ def auth_switch(plugin, data)

@sock.read_packet do |packet|
if packet.err?
code, message = read_error(packet)
code, message = Protocol.read_error(packet)
raise ConnectionError, "#{code}: #{message}"
end
end
Expand All @@ -107,15 +107,6 @@ def mysql_native_password(scramble)

bytes.pack("C*")
end

def read_error(packet)
packet.int
code = packet.int(2)
packet.strn(1)
packet.strn(5)
message = packet.eof_str
[code, message]
end
end
end
end
3 changes: 3 additions & 0 deletions lib/nocturne/protocol/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def query(sql)
@sock.read_packet do |payload|
if payload.ok?
# Done. No results.
elsif payload.err?
code, message = Protocol.read_error(payload)
raise QueryError, "#{code}: #{message}"
else
column_count = payload.lenenc_int
end
Expand Down

0 comments on commit 88262cb

Please sign in to comment.