Skip to content

Commit 1817d0f

Browse files
committed
Added ServerException for easier error handling (related to #37)
1 parent e8b73f1 commit 1817d0f

File tree

6 files changed

+4628
-12
lines changed

6 files changed

+4628
-12
lines changed

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

+15-7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.github.shyiko.mysql.binlog.io.ByteArrayInputStream;
3232
import com.github.shyiko.mysql.binlog.jmx.BinaryLogClientMXBean;
3333
import com.github.shyiko.mysql.binlog.network.AuthenticationException;
34+
import com.github.shyiko.mysql.binlog.network.ServerException;
3435
import com.github.shyiko.mysql.binlog.network.SocketFactory;
3536
import com.github.shyiko.mysql.binlog.network.protocol.ErrorPacket;
3637
import com.github.shyiko.mysql.binlog.network.protocol.GreetingPacket;
@@ -310,7 +311,8 @@ public void setThreadFactory(ThreadFactory threadFactory) {
310311

311312
/**
312313
* Connect to the replication stream. Note that this method blocks until disconnected.
313-
* @throws AuthenticationException in case of failed authentication
314+
* @throws AuthenticationException if authentication fails
315+
* @throws ServerException if MySQL server responds with an error
314316
* @throws IOException if anything goes wrong while trying to connect
315317
*/
316318
public void connect() throws IOException {
@@ -327,7 +329,7 @@ public void connect() throws IOException {
327329
}
328330
} catch (IOException e) {
329331
throw new IOException("Failed to connect to MySQL on " + hostname + ":" + port +
330-
". Please make sure it's running.", e);
332+
". Please make sure it's running.", e);
331333
}
332334
GreetingPacket greetingPacket = new GreetingPacket(channel.read());
333335
authenticate(greetingPacket.getScramble(), greetingPacket.getServerCollation());
@@ -409,7 +411,9 @@ private void authenticate(String salt, int collation) throws IOException {
409411
if (authenticationResult[0] != (byte) 0x00 /* ok */) {
410412
if (authenticationResult[0] == (byte) 0xFF /* error */) {
411413
byte[] bytes = Arrays.copyOfRange(authenticationResult, 1, authenticationResult.length);
412-
throw new AuthenticationException(new ErrorPacket(bytes).getErrorMessage());
414+
ErrorPacket errorPacket = new ErrorPacket(bytes);
415+
throw new AuthenticationException(errorPacket.getErrorMessage(), errorPacket.getErrorCode(),
416+
errorPacket.getSqlState());
413417
}
414418
throw new AuthenticationException("Unexpected authentication result (" + authenticationResult[0] + ")");
415419
}
@@ -476,9 +480,10 @@ boolean isKeepAliveThreadRunning() {
476480
/**
477481
* Connect to the replication stream in a separate thread.
478482
* @param timeoutInMilliseconds timeout in milliseconds
479-
* @throws AuthenticationException in case of failed authentication
483+
* @throws AuthenticationException if authentication fails
484+
* @throws ServerException if MySQL server responds with an error
480485
* @throws IOException if anything goes wrong while trying to connect
481-
* @throws TimeoutException if client wasn't able to connect in the requested period of time
486+
* @throws TimeoutException if client was unable to connect within given time limit
482487
*/
483488
public void connect(long timeoutInMilliseconds) throws IOException, TimeoutException {
484489
final CountDownLatch countDownLatch = new CountDownLatch(1);
@@ -553,7 +558,9 @@ private void confirmSupportOfChecksum(ChecksumType checksumType) throws IOExcept
553558
byte[] statementResult = channel.read();
554559
if (statementResult[0] == (byte) 0xFF /* error */) {
555560
byte[] bytes = Arrays.copyOfRange(statementResult, 1, statementResult.length);
556-
throw new IOException(new ErrorPacket(bytes).getErrorMessage());
561+
ErrorPacket errorPacket = new ErrorPacket(bytes);
562+
throw new ServerException(errorPacket.getErrorMessage(), errorPacket.getErrorCode(),
563+
errorPacket.getSqlState());
557564
}
558565
eventDeserializer.setChecksumType(checksumType);
559566
}
@@ -567,7 +574,8 @@ private void listenForEventPackets() throws IOException {
567574
int marker = inputStream.read();
568575
if (marker == 0xFF) {
569576
ErrorPacket errorPacket = new ErrorPacket(inputStream.read(packetLength - 1));
570-
throw new IOException(errorPacket.getErrorCode() + " - " + errorPacket.getErrorMessage());
577+
throw new ServerException(errorPacket.getErrorMessage(), errorPacket.getErrorCode(),
578+
errorPacket.getSqlState());
571579
}
572580
Event event;
573581
try {

src/main/java/com/github/shyiko/mysql/binlog/network/AuthenticationException.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515
*/
1616
package com.github.shyiko.mysql.binlog.network;
1717

18-
import java.io.IOException;
19-
2018
/**
2119
* @author <a href="mailto:stanley.shyiko@gmail.com">Stanley Shyiko</a>
2220
*/
23-
public class AuthenticationException extends IOException {
21+
public class AuthenticationException extends ServerException {
22+
23+
public AuthenticationException(String message, int errorCode, String sqlState) {
24+
super(message, errorCode, sqlState);
25+
}
2426

2527
public AuthenticationException(String message) {
26-
super(message);
28+
super(message, 0, "HY000");
2729
}
2830
}

0 commit comments

Comments
 (0)