Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error reporting for loading invalid games #191

Merged
merged 1 commit into from
Dec 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading