Skip to content

Commit 0132265

Browse files
authored
Make the exception message when unable to connect to the server more descriptive (#236)
## What is the goal of this PR? Exception message when client cannot connect to server was not descriptive enough ("io exception"). It's changed to a more user-friendly message ("Unable to connect to Grakn Core Server"). ## What are the changes implemented in this PR? Handle the special case of gRPC error - `UNAVAILABLE` (code 14) by supplying helpful `ErrorMessage`. Additionally, store and expose that `ErrorMessage` in `GraknClientException` such that user can distinguish the errors.
1 parent 7b3795d commit 0132265

File tree

5 files changed

+23
-7
lines changed

5 files changed

+23
-7
lines changed

common/exception/ErrorMessage.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public static class Client extends ErrorMessage {
4343

4444
public static final Client CLUSTER_NOT_AVAILABLE =
4545
new Client(8, "Attempted connecting to these servers, but none are available: '%s'.");
46+
public static final Client UNABLE_TO_CONNECT =
47+
new Client(9, "Unable to connect to Grakn Core Server.");
4648

4749
private static final String codePrefix = "CLI";
4850
private static final String messagePrefix = "Illegal Client State";

common/exception/GraknClientException.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,44 @@
1919

2020
package grakn.client.common.exception;
2121

22+
import io.grpc.Status;
2223
import io.grpc.StatusRuntimeException;
2324

2425
import javax.annotation.Nullable;
2526

2627
public class GraknClientException extends RuntimeException {
2728

29+
@Nullable private final ErrorMessage errorMessage;
30+
2831
public GraknClientException(String error) {
2932
super(error);
33+
this.errorMessage = null;
3034
}
3135

3236
public GraknClientException(ErrorMessage error) {
3337
super(error.toString());
3438
assert !getMessage().contains("%s");
39+
this.errorMessage = error;
3540
}
3641

37-
public GraknClientException(StatusRuntimeException statusRuntimeException) {
38-
super(statusRuntimeException.getStatus().getDescription());
42+
public static GraknClientException of(StatusRuntimeException statusRuntimeException) {
43+
if (statusRuntimeException.getStatus().getCode() == Status.Code.UNAVAILABLE) {
44+
return new GraknClientException(ErrorMessage.Client.UNABLE_TO_CONNECT);
45+
}
46+
return new GraknClientException(statusRuntimeException.getStatus().getDescription());
3947
}
4048

4149
public GraknClientException(Exception e) {
4250
super(e);
51+
this.errorMessage = null;
4352
}
4453

4554
public String getName() {
4655
return this.getClass().getName();
4756
}
57+
58+
@Nullable
59+
public ErrorMessage getErrorMessage() {
60+
return errorMessage;
61+
}
4862
}

rpc/RPCDatabaseManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private static <RES> RES request(Supplier<RES> req) {
7171
try {
7272
return req.get();
7373
} catch (StatusRuntimeException e) {
74-
throw new GraknClientException(e);
74+
throw GraknClientException.of(e);
7575
}
7676
}
7777
}

rpc/RPCSession.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public Core(GraknClient.Core client, String database, Type type, GraknOptions op
7272
isOpen = new AtomicBoolean(true);
7373
pulse.scheduleAtFixedRate(this.new PulseTask(), 0, 5000);
7474
} catch (StatusRuntimeException e) {
75-
throw new GraknClientException(e);
75+
throw GraknClientException.of(e);
7676
}
7777
}
7878

@@ -105,7 +105,7 @@ public void close() {
105105
try {
106106
blockingGrpcStub.sessionClose(SessionProto.Session.Close.Req.newBuilder().setSessionId(sessionId).build());
107107
} catch (StatusRuntimeException e) {
108-
throw new GraknClientException(e);
108+
throw GraknClientException.of(e);
109109
}
110110
}
111111
}

rpc/RPCTransaction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public class RPCTransaction implements Transaction {
8484
final Instant endTime = Instant.now();
8585
networkLatencyMillis = (int) ChronoUnit.MILLIS.between(startTime, endTime) - res.getProcessingTimeMillis();
8686
} catch (StatusRuntimeException e) {
87-
throw new GraknClientException(e);
87+
throw GraknClientException.of(e);
8888
}
8989
}
9090

@@ -144,7 +144,7 @@ private void close(Response.Done doneResponse) {
144144
try {
145145
requestObserver.onCompleted();
146146
} catch (StatusRuntimeException e) {
147-
throw new GraknClientException(e);
147+
throw GraknClientException.of(e);
148148
}
149149
}
150150
}

0 commit comments

Comments
 (0)