Skip to content

Commit

Permalink
Add error reporting for loading invalid games
Browse files Browse the repository at this point in the history
  • Loading branch information
haykam821 authored and Patbox committed Dec 8, 2024
1 parent d5d0c7b commit 3fe8614
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion src/main/java/xyz/nucleoid/extras/error/ExtrasErrorReporter.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package xyz.nucleoid.extras.error;

import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import net.minecraft.util.crash.CrashReport;
import net.minecraft.util.crash.ReportType;
import org.jetbrains.annotations.Nullable;
Expand All @@ -10,8 +12,10 @@
import xyz.nucleoid.plasmid.api.game.GameCloseReason;
import xyz.nucleoid.plasmid.api.game.GameLifecycle;
import xyz.nucleoid.plasmid.api.game.GameSpace;
import xyz.nucleoid.plasmid.api.game.GameType;
import xyz.nucleoid.plasmid.api.game.config.GameConfig;
import xyz.nucleoid.plasmid.api.game.config.GameConfigs;
import xyz.nucleoid.plasmid.impl.Plasmid;

import java.io.IOException;
import java.io.PrintWriter;
Expand All @@ -21,13 +25,35 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

public final class ExtrasErrorReporter {
public static final CustomErrorType TOO_FAST = new CustomErrorType(Duration.ofMinutes(1));
public static final CustomErrorType INVALID_GAMES = new CustomErrorType(Duration.ofMinutes(1));

private static final Map<CustomErrorType, Instant> LAST_REPORT = new ConcurrentHashMap<>();

public static void register() {
ServerLifecycleEvents.SERVER_STARTED.register(server -> {
var invalidType = GameType.get(Identifier.of(Plasmid.ID, "invalid"));

var invalidGames = server.getRegistryManager()
.getOrThrow(GameConfigs.REGISTRY_KEY)
.streamEntries()
.filter(entry -> entry.value().type() == invalidType)
.map(ExtrasErrorReporter::sourceName)
.map(entry -> " - " + entry)
.sorted()
.toList();

if (!invalidGames.isEmpty()) {
var lines = String.join("\n", invalidGames);
var trace = String.format("Loaded %d invalid game(s):\n\n%s", invalidGames.size(), lines);

reportCustom(INVALID_GAMES, trace);
}
});

GameEvents.OPENED.register((game, gameSpace) -> {
var config = NucleoidExtrasConfig.get();
var errorReporting = config.errorReporting();
Expand Down Expand Up @@ -67,14 +93,26 @@ public static void onServerCrash(CrashReport report) {
}

public static void reportCustom(CustomErrorType type, Throwable throwable) {
reportCustom(type, writer -> {
throwable.printStackTrace(new PrintWriter(writer));
});
}

public static void reportCustom(CustomErrorType type, String trace) {
reportCustom(type, writer -> {
writer.write(trace);
});
}

private static void reportCustom(CustomErrorType type, Consumer<StringWriter> trace) {
if (!updateLastReportTime(type, Instant.now())) {
return;
}
var webhook = openWebhook();
if (webhook != null) {
var message = new DiscordWebhook.Message("An error has occurred!");
try (var writer = new StringWriter()) {
throwable.printStackTrace(new PrintWriter(writer));
trace.accept(writer);
message.addFile("trace.txt", writer.toString());
webhook.post(message);
} catch (IOException e) {
Expand Down

0 comments on commit 3fe8614

Please sign in to comment.