Try-catch MojangUtils#getName and a portion of POST /server/heartbeat to make debugging easier

This commit is contained in:
Colin McDonald 2016-06-21 00:58:49 -04:00
parent 27b98d3b69
commit 3bc703384b
2 changed files with 36 additions and 27 deletions

View File

@ -107,32 +107,36 @@ public final class POSTServerHeartbeat implements Handler<RoutingContext> {
if (result.failed()) { if (result.failed()) {
callback.fail(result.cause()); callback.fail(result.cause());
} else { } else {
Map<UUID, User> users = result.result().result(0); try {
Map<UUID, List<Grant>> grants = result.result().result(1); Map<UUID, User> users = result.result().result(0);
Map<UUID, List<Punishment>> punishments = result.result().result(2); Map<UUID, List<Grant>> grants = result.result().result(1);
Map<String, Object> response = new HashMap<>(); Map<UUID, List<Punishment>> punishments = result.result().result(2);
Map<String, Object> response = new HashMap<>();
for (Map.Entry<UUID, User> userEntry : users.entrySet()) { for (Map.Entry<UUID, User> userEntry : users.entrySet()) {
UUID uuid = userEntry.getKey(); UUID uuid = userEntry.getKey();
User user = userEntry.getValue(); User user = userEntry.getValue();
if (user == null) { if (user == null) {
String username = playerNames.get(uuid); String username = playerNames.get(uuid);
user = new User(uuid, username); user = new User(uuid, username);
user.insert(); user.insert();
users.put(uuid, user); users.put(uuid, user);
}
// Only save if needed
if (user.seenOnServer(server)) {
user.save();
}
// TODO: Provide IPs for ip ban lookup
response.put(uuid.toString(), user.createLoginInfo(server, punishments.get(uuid), ImmutableList.of(), grants.get(uuid)));
} }
// Only save if needed callback.complete(response);
if (user.seenOnServer(server)) { } catch (Exception ex) {
user.save(); callback.fail(ex);
}
// TODO: Provide IPs for ip ban lookup
response.put(uuid.toString(), user.createLoginInfo(server, punishments.get(uuid), ImmutableList.of(), grants.get(uuid)));
} }
callback.complete(response);
} }
}); });

View File

@ -4,6 +4,7 @@ import com.mongodb.async.SingleResultCallback;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.APIv3;
import org.bson.Document; import org.bson.Document;
import org.bson.json.JsonParseException;
import java.io.IOException; import java.io.IOException;
import java.util.UUID; import java.util.UUID;
@ -14,13 +15,17 @@ public class MojangUtils {
public static void getName(UUID id, SingleResultCallback<String> callback) { public static void getName(UUID id, SingleResultCallback<String> callback) {
APIv3.getHttpClient().get("sessionserver.mojang.com", "/session/minecraft/profile/" + id.toString().replace("-", ""), (response) -> { APIv3.getHttpClient().get("sessionserver.mojang.com", "/session/minecraft/profile/" + id.toString().replace("-", ""), (response) -> {
response.bodyHandler((body) -> { response.bodyHandler((body) -> {
Document resJson = Document.parse(body.toString()); try {
String name = resJson.getString("name"); Document resJson = Document.parse(body.toString());
String name = resJson.getString("name");
if (name == null) { if (name == null) {
callback.onResult(null, new IOException("Hit Mojang API rate limit: " + resJson.toJson())); callback.onResult(null, new IOException("Hit Mojang API rate limit: " + resJson.toJson()));
} else { } else {
callback.onResult(name, null); callback.onResult(name, null);
}
} catch (JsonParseException ex) {
callback.onResult(null, new RuntimeException(body.toString(), ex));
} }
}); });