Skip to content

Commit 1b6b3dc

Browse files
Improve error handling to be more defined (#5)
1 parent dc7137b commit 1b6b3dc

File tree

5 files changed

+102
-2
lines changed

5 files changed

+102
-2
lines changed

src/main/java/net/hypixel/modapi/HypixelModAPI.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package net.hypixel.modapi;
22

3+
import net.hypixel.modapi.error.ErrorReason;
4+
import net.hypixel.modapi.error.ModAPIException;
35
import net.hypixel.modapi.handler.ClientboundPacketHandler;
46
import net.hypixel.modapi.packet.HypixelPacket;
57
import net.hypixel.modapi.packet.HypixelPacketType;
@@ -36,8 +38,8 @@ public void handle(String identifier, PacketSerializer serializer) {
3638

3739
// All responses contain a boolean of if the response is a success, if not then a string is included with the error message
3840
if (!serializer.readBoolean()) {
39-
String errorMessage = serializer.readString();
40-
throw new RuntimeException("Received error response for packet " + packetType + ": " + errorMessage);
41+
ErrorReason reason = ErrorReason.getById(serializer.readVarInt());
42+
throw new ModAPIException(packetType, reason);
4143
}
4244

4345
HypixelPacket packet = packetType.getPacketFactory().apply(serializer);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package net.hypixel.modapi.error;
2+
3+
import java.util.Arrays;
4+
import java.util.Map;
5+
import java.util.function.Function;
6+
import java.util.stream.Collectors;
7+
8+
public enum BuiltinErrorReason implements ErrorReason {
9+
DISABLED(1),
10+
INTERNAL_SERVER_ERROR(2),
11+
RATE_LIMITED(3),
12+
INVALID_PACKET_VERSION(4),
13+
NO_LONGER_SUPPORTED(5),
14+
;
15+
16+
private static final Map<Integer, BuiltinErrorReason> BY_ID = Arrays.stream(values())
17+
.collect(Collectors.toMap(BuiltinErrorReason::getId, Function.identity()));
18+
19+
static BuiltinErrorReason getById(int id) {
20+
return BY_ID.get(id);
21+
}
22+
23+
private final int id;
24+
25+
BuiltinErrorReason(int id) {
26+
this.id = id;
27+
}
28+
29+
@Override
30+
public int getId() {
31+
return id;
32+
}
33+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package net.hypixel.modapi.error;
2+
3+
public interface ErrorReason {
4+
int getId();
5+
6+
static ErrorReason getById(int id) {
7+
BuiltinErrorReason reason = BuiltinErrorReason.getById(id);
8+
if (reason != null) {
9+
return reason;
10+
}
11+
12+
return new UnknownErrorReason(id);
13+
}
14+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package net.hypixel.modapi.error;
2+
3+
import net.hypixel.modapi.packet.HypixelPacketType;
4+
5+
public class ModAPIException extends RuntimeException {
6+
private final HypixelPacketType packetType;
7+
private final ErrorReason reason;
8+
9+
public ModAPIException(HypixelPacketType packetType, ErrorReason reason) {
10+
super(String.format("Received error response '%s' from packet '%s'", reason, packetType));
11+
this.packetType = packetType;
12+
this.reason = reason;
13+
}
14+
15+
public HypixelPacketType getPacketType() {
16+
return packetType;
17+
}
18+
19+
public ErrorReason getReason() {
20+
return reason;
21+
}
22+
23+
@Override
24+
public String toString() {
25+
return "ModAPIException{" +
26+
"packetType=" + packetType +
27+
", reason=" + reason +
28+
"} " + super.toString();
29+
}
30+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package net.hypixel.modapi.error;
2+
3+
public class UnknownErrorReason implements ErrorReason {
4+
private final int id;
5+
6+
UnknownErrorReason(int id) {
7+
this.id = id;
8+
}
9+
10+
@Override
11+
public int getId() {
12+
return id;
13+
}
14+
15+
@Override
16+
public String toString() {
17+
return "UnknownErrorReason{" +
18+
"id=" + id +
19+
'}';
20+
}
21+
}

0 commit comments

Comments
 (0)