Added KERNEL PANIC message when commands crash unexpectedly, so I can take a look at it later, and it doesn't break the command

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-11-10 12:44:08 +01:00
parent c134e87d89
commit a028dab281
9 changed files with 26 additions and 11 deletions

View File

@ -11,7 +11,7 @@ public class MoviesQuoteBot {
public static final String PREFIX = "!"; public static final String PREFIX = "!";
public static final String NAME = "Movies Quote Bot"; public static final String NAME = "Movies Quote Bot";
public static final String DESCRIPTION = "I may know some quotes from some movies."; public static final String DESCRIPTION = "I may know some quotes from some movies.";
public static final String VERSION = "0.3.4-SNAPSHOT"; public static final String VERSION = "0.3.5-SNAPSHOT";
private static final Logger logger = LoggerFactory.getLogger(MoviesQuoteBot.class.getCanonicalName()); private static final Logger logger = LoggerFactory.getLogger(MoviesQuoteBot.class.getCanonicalName());
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -6,6 +6,7 @@ import discord4j.core.object.reaction.ReactionEmoji;
import discord4j.core.spec.EmbedCreateSpec; import discord4j.core.spec.EmbedCreateSpec;
import discord4j.rest.util.Color; import discord4j.rest.util.Color;
import org.apache.commons.cli.Options; import org.apache.commons.cli.Options;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
@ -14,6 +15,7 @@ import xyz.vallat.louis.omdb.objects.Movie;
import xyz.vallat.louis.subtitles.OpenSubtitles; import xyz.vallat.louis.subtitles.OpenSubtitles;
import java.io.IOException; import java.io.IOException;
import java.time.Instant;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function; import java.util.function.Function;
@ -22,8 +24,9 @@ import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
public abstract class Command { public abstract class Command {
protected static final ReactionEmoji WAITING = ReactionEmoji.unicode(""); protected static final ReactionEmoji WAITING = ReactionEmoji.unicode("");
private static final Logger logger = LoggerFactory.getLogger(ListLang.class.getCanonicalName()); private static final Logger logger = LoggerFactory.getLogger(Command.class.getCanonicalName());
protected final String name; protected final String name;
protected final String description; protected final String description;
protected final String usage; protected final String usage;
@ -81,6 +84,19 @@ public abstract class Command {
.then(); .then();
} }
protected Mono<Void> fatalError(MessageCreateEvent event, Throwable throwable) {
return event.getMessage().getChannel().flatMap(
channel -> channel.createEmbed(embed -> {
event.getMessage().removeSelfReaction(WAITING);
String errorCode = RandomStringUtils.random(8, true, true);
logger.error("KERNEL PANIC: {}", errorCode, throwable);
embed.setTitle("KERNEL PANIC").setColor(Color.RED)
.setDescription("A fatal and unhandled error has been encountered. " +
"Please transfer this message to my administrator.")
.addField("IDENTIFIER", errorCode, false).setTimestamp(Instant.now());
})).then();
}
protected void createEmbedListLang(String arg, EmbedCreateSpec embed, boolean isId) { protected void createEmbedListLang(String arg, EmbedCreateSpec embed, boolean isId) {
try { try {
Movie movie = OMDBClient.getMovie(arg, isId); Movie movie = OMDBClient.getMovie(arg, isId);

View File

@ -69,7 +69,8 @@ public class Download extends Command {
} }
embed.setTimestamp(Instant.now()); embed.setTimestamp(Instant.now());
} }
))).then(event.getMessage().removeSelfReaction(WAITING)).then(); ))).then(event.getMessage().removeSelfReaction(WAITING)).then()
.onErrorResume(throwable -> fatalError(event, throwable));
} catch (ParseException e) { } catch (ParseException e) {
logger.debug("Parsing error: {}", e.getMessage()); logger.debug("Parsing error: {}", e.getMessage());
return parsingError(event); return parsingError(event);

View File

@ -22,7 +22,7 @@ public class Help extends Command {
embed.addField(command.getName(), embed.addField(command.getName(),
command.getDescription() + " Usage: " + command.getUsage() + ".", false); command.getDescription() + " Usage: " + command.getUsage() + ".", false);
} }
)).then(); )).then().onErrorResume(throwable -> fatalError(event, throwable));
} }
} }

View File

@ -46,7 +46,8 @@ public class ListLang extends Command {
embed.setDescription("I cannot search for subtitle languages right now. Sorry."); embed.setDescription("I cannot search for subtitle languages right now. Sorry.");
} }
embed.setTimestamp(Instant.now()); embed.setTimestamp(Instant.now());
})))).then(event.getMessage().removeSelfReaction(WAITING)).then(); })))).then(event.getMessage().removeSelfReaction(WAITING)).then()
.onErrorResume(throwable -> fatalError(event, throwable));
} catch (ParseException e) { } catch (ParseException e) {
logger.debug("Parsing error: {}", e.getMessage()); logger.debug("Parsing error: {}", e.getMessage());
return parsingError(event); return parsingError(event);

View File

@ -13,6 +13,6 @@ public class Ping extends Command {
public Mono<Void> execute(MessageCreateEvent event) { public Mono<Void> execute(MessageCreateEvent event) {
return event.getMessage().getChannel() return event.getMessage().getChannel()
.flatMap(channel -> channel.createMessage("Pong!")) .flatMap(channel -> channel.createMessage("Pong!"))
.then(); .then().onErrorResume(throwable -> fatalError(event, throwable));
} }
} }

View File

@ -61,7 +61,7 @@ public class Quote extends Command {
} }
} }
embed.setTimestamp(Instant.now()); embed.setTimestamp(Instant.now());
})).then(); })).then().onErrorResume(throwable -> fatalError(event, throwable));
} catch (ParseException e) { } catch (ParseException e) {
logger.debug("Parsing error: {}", e.getMessage()); logger.debug("Parsing error: {}", e.getMessage());
return parsingError(event); return parsingError(event);

View File

@ -45,7 +45,7 @@ public class Version extends Command {
} }
) )
) )
.then(); .then().onErrorResume(throwable -> fatalError(event, throwable));
} }
} }

View File

@ -1,6 +1,5 @@
package xyz.vallat.louis.omdb; package xyz.vallat.louis.omdb;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -17,8 +16,6 @@ import java.net.http.HttpClient;
import java.net.http.HttpRequest; import java.net.http.HttpRequest;
import java.net.http.HttpResponse; import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
public final class OMDBClient { public final class OMDBClient {