Skip to content

Commit c39539d

Browse files
committed
Improved initial handshake error reporting (by including actual error message from MySQL)
1 parent 51d2494 commit c39539d

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/main/java/com/github/shyiko/mysql/binlog/BinaryLogClient.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,14 @@ public void connect() throws IOException {
331331
throw new IOException("Failed to connect to MySQL on " + hostname + ":" + port +
332332
". Please make sure it's running.", e);
333333
}
334-
GreetingPacket greetingPacket = new GreetingPacket(channel.read());
334+
byte[] initialHandshakePacket = channel.read();
335+
if (initialHandshakePacket[0] == (byte) 0xFF /* error */) {
336+
byte[] bytes = Arrays.copyOfRange(initialHandshakePacket, 1, initialHandshakePacket.length);
337+
ErrorPacket errorPacket = new ErrorPacket(bytes);
338+
throw new ServerException(errorPacket.getErrorMessage(), errorPacket.getErrorCode(),
339+
errorPacket.getSqlState());
340+
}
341+
GreetingPacket greetingPacket = new GreetingPacket(initialHandshakePacket);
335342
authenticate(greetingPacket.getScramble(), greetingPacket.getServerCollation());
336343
if (binlogFilename == null) {
337344
fetchBinlogFilenameAndPosition();

src/main/java/com/github/shyiko/mysql/binlog/network/protocol/ErrorPacket.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ public class ErrorPacket implements Packet {
3131
public ErrorPacket(byte[] bytes) throws IOException {
3232
ByteArrayInputStream buffer = new ByteArrayInputStream(bytes);
3333
this.errorCode = buffer.readInteger(2);
34-
buffer.skip(1); // 1 byte for slash
35-
this.sqlState = buffer.readString(5);
34+
if (buffer.peek() == '#') {
35+
buffer.skip(1); // marker of the SQL State
36+
this.sqlState = buffer.readString(5);
37+
}
3638
this.errorMessage = buffer.readString(buffer.available());
3739
}
3840

0 commit comments

Comments
 (0)