-
Notifications
You must be signed in to change notification settings - Fork 0
Error Handling
mega12345mega edited this page Jan 25, 2023
·
1 revision
When an error occurs, the corresponding error handlers will be called. If there are no error handlers, the exception will be printed. Error handlers are all called on the same thread, one after the other, causing the caller to wait. Error handlers are passed the Exception, Object where the error occurred, and the Error.
The Error gives more details on where the error occurred, and you can call Error#getCloseInfo to check how the error affected the server, server connection, or client.
Use Server#addServerErrorHandler, Server#addConnectionErrorHandler, ServerConnection#addErrorHandler, and Client#addErrorHandler to add an error handler to the correct class.
Example:
server.addConnectionErrorHandler((e, conn, error) -> {
if (error == ErrorHandler.Error.UNREGISTERED_PACKET)
conn.sendPacket(new PrimitivePacket("Your packet registry doesn't match mine!"));
else if (error == ErrorHandler.Error.CONSTRUCTING_PACKET)
conn.sendPacket(new PrimitivePacket("You sent a malformed packet!"));
else {
try {
conn.sendPacket(new DisconnectPacket("Unexpected error: " + error));
conn.close();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
});