Skip to content

Commit f0c9e0e

Browse files
committed
Nicer error codes
1 parent fa08088 commit f0c9e0e

3 files changed

Lines changed: 22 additions & 10 deletions

File tree

api/src/main/java/xyz/gianlu/librespot/api/MercuryHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public MercuryHandler(@NotNull Session session) {
5454
obj.add("payloads", payloads);
5555
return obj;
5656
} catch (IOException ex) {
57-
throw new HandlingException(ex, 100 /* FIXME */);
57+
throw new HandlingException(ex, ErrorCode.IO_EXCEPTION);
5858
}
5959
} else {
6060
throw ApiServer.PredefinedJsonRpcException.from(request, ApiServer.PredefinedJsonRpcError.METHOD_NOT_FOUND);

api/src/main/java/xyz/gianlu/librespot/api/MetadataHandler.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@ public MetadataHandler(@NotNull Session session) {
4545
private <P extends AbstractMessageLite> JsonElement handle(@NotNull ProtoJsonMercuryRequest<P> req) throws HandlingException {
4646
try {
4747
return client.sendSync(req).json();
48-
} catch (IOException | MercuryClient.MercuryException ex) {
49-
throw new HandlingException(ex, 100); // FIXME: Create error codes table
48+
} catch (MercuryClient.MercuryException ex) {
49+
throw new HandlingException(ex, ErrorCode.MERCURY_EXCEPTION);
50+
} catch (IOException ex) {
51+
throw new HandlingException(ex, ErrorCode.IO_EXCEPTION);
5052
}
5153
}
5254

5355
@Override
5456
protected void handleNotification(ApiServer.@NotNull Request request) {
55-
5657
}
5758
}

api/src/main/java/xyz/gianlu/librespot/api/server/AbsApiHandler.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ final void handle(@NotNull ApiServer.Request request) {
3535
JsonElement result = handleRequest(request);
3636
request.answerResult(result);
3737
} catch (HandlingException ex) {
38-
request.answerError(ex.code, ex.msg, ex.data);
38+
request.answerError(ex.code.code, ex.msg, ex.data);
3939
} catch (ApiServer.PredefinedJsonRpcException ex) {
4040
request.answerError(ex);
4141
}
@@ -46,30 +46,41 @@ final void handle(@NotNull ApiServer.Request request) {
4646

4747
protected abstract void handleNotification(@NotNull ApiServer.Request request);
4848

49+
public enum ErrorCode {
50+
MERCURY_EXCEPTION(1001),
51+
IO_EXCEPTION(1002);
52+
53+
public final int code;
54+
55+
ErrorCode(int code) {
56+
this.code = code;
57+
}
58+
}
59+
4960
protected static class HandlingException extends Exception {
50-
private final int code;
61+
private final ErrorCode code;
5162
private final String msg;
5263
private final JsonElement data;
5364

54-
public HandlingException(int code, @NotNull String msg, @Nullable JsonElement data) {
65+
public HandlingException(@NotNull ErrorCode code, @NotNull String msg, @Nullable JsonElement data) {
5566
super(msg);
5667
this.code = code;
5768
this.msg = msg;
5869
this.data = data;
5970
}
6071

61-
public HandlingException(int code, @NotNull String msg) {
72+
public HandlingException(@NotNull ErrorCode code, @NotNull String msg) {
6273
this(code, msg, null);
6374
}
6475

65-
public HandlingException(@NotNull Throwable cause, int code, @Nullable JsonElement data) {
76+
public HandlingException(@NotNull Throwable cause, @NotNull ErrorCode code, @Nullable JsonElement data) {
6677
super(cause);
6778
this.code = code;
6879
this.msg = cause.getMessage();
6980
this.data = data;
7081
}
7182

72-
public HandlingException(@NotNull Throwable cause, int code) {
83+
public HandlingException(@NotNull Throwable cause, @NotNull ErrorCode code) {
7384
this(cause, code, null);
7485
}
7586
}

0 commit comments

Comments
 (0)