Browse Source

catch handshake errors

Fixes issue #36
Julien Schmidt 13 years ago
parent
commit
e6e220509b
1 changed files with 11 additions and 2 deletions
  1. 11 2
      packets.go

+ 11 - 2
packets.go

@@ -91,6 +91,10 @@ func (mc *mysqlConn) readInitPacket() (err error) {
 		return
 		return
 	}
 	}
 
 
+	if data[0] == 255 {
+		return mc.handleErrorPacket(data)
+	}
+
 	// protocol version [1 byte]
 	// protocol version [1 byte]
 	if data[0] < minProtocolVersion {
 	if data[0] < minProtocolVersion {
 		err = fmt.Errorf(
 		err = fmt.Errorf(
@@ -340,11 +344,16 @@ func (mc *mysqlConn) handleErrorPacket(data []byte) error {
 	// Error Number [16 bit uint]
 	// Error Number [16 bit uint]
 	errno := binary.LittleEndian.Uint16(data[1:3])
 	errno := binary.LittleEndian.Uint16(data[1:3])
 
 
-	// SQL State [# + 5bytes string]
+	pos := 3
+
+	// SQL State [optional: # + 5bytes string]
 	//sqlstate := string(data[pos : pos+6])
 	//sqlstate := string(data[pos : pos+6])
+	if data[pos] == 0x23 {
+		pos = 9
+	}
 
 
 	// Error Message [string]
 	// Error Message [string]
-	return fmt.Errorf("Error %d: %s", errno, string(data[9:]))
+	return fmt.Errorf("Error %d: %s", errno, string(data[pos:]))
 }
 }
 
 
 // Ok Packet
 // Ok Packet