Skip to content

Commit 8b2d3c5

Browse files
authored
fix(logger): friendlier handling of default values (#1263)
I have noticed that when inexperienced people just getting started on the project go through all the hoops of cloning the repository, setting up their Discord bots, creating their test server, they end up compiling the code in IDEs like IntelliJ IDEA and assuming they just change the token on their `config.json`, there are stacktraces and errors thrown once they attempt to run the bot. Specifically: Caused by: java.net.URISyntaxException: Illegal character in path at index 0: <put_your_webhook_here> at java.base/java.net.URI$Parser.fail(URI.java:2995) at java.base/java.net.URI$Parser.checkChars(URI.java:3166) at java.base/java.net.URI$Parser.parseHierarchical(URI.java:3248) at java.base/java.net.URI$Parser.parse(URI.java:3207) at java.base/java.net.URI.<init>(URI.java:645) at java.base/java.net.URI.create(URI.java:930) For the average software engineer, they would be aware that the program is still running and that the bot is ready, but two things: - Someone who is not trained enough and is just starting to contribute will get thrown off by the error and potentially give up (I have experienced it happening) - It's a generally better experience for knowledgable new contributors to set up the project effortlessly so that they can get started with contributing straight away without any stacktraces. This commit prints a friendly warning in the logger and no stacktrace in case the user has not updated the webhooks (which most of them will not have to, as they are primarily concerned with the Discord bot's token). We hide the stacktrace in case there is a "<put_your_webhook_here>" value in the "logInfoChannelWebhook" and "logErrorChannelWebhook" configuration keys so that IntelliJ IDEA does not put the stacktrace in the spotlight and potentially scare new contributors. Signed-off-by: christolis <[email protected]>
1 parent 4f03aa8 commit 8b2d3c5

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

application/src/main/java/org/togetherjava/tjbot/logging/discord/DiscordLogging.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
public final class DiscordLogging {
2525
private static final Logger logger = LoggerFactory.getLogger(DiscordLogging.class);
26+
private static final String LOG_CHANNEL_WEBHOOK_DEFAULT_VALUE = "<put_your_webhook_here>";
2627

2728
private DiscordLogging() {
2829
throw new UnsupportedOperationException("Utility class");
@@ -45,22 +46,29 @@ public static void startDiscordLogging(Config botConfig) {
4546
}
4647

4748
private static void addAppenders(Configuration logConfig, Config botConfig) {
48-
parseWebhookUri(botConfig.getLogInfoChannelWebhook())
49+
parseWebhookUri(botConfig.getLogInfoChannelWebhook(), "info")
4950
.ifPresent(webhookUri -> addDiscordLogAppender("DiscordInfo", createInfoRangeFilter(),
5051
webhookUri, botConfig.getSourceCodeBaseUrl(), logConfig));
5152

52-
parseWebhookUri(botConfig.getLogErrorChannelWebhook())
53+
parseWebhookUri(botConfig.getLogErrorChannelWebhook(), "error")
5354
.ifPresent(webhookUri -> addDiscordLogAppender("DiscordError", createErrorRangeFilter(),
5455
webhookUri, botConfig.getSourceCodeBaseUrl(), logConfig));
5556
}
5657

57-
private static Optional<URI> parseWebhookUri(String webhookUri) {
58+
private static Optional<URI> parseWebhookUri(String webhookUri, String webhookName) {
59+
if (webhookUri.equalsIgnoreCase(LOG_CHANNEL_WEBHOOK_DEFAULT_VALUE)) {
60+
logger.warn(
61+
"The {} webhook URL was not setup yet, logs will not be forwarded to Discord. Enter a valid webhook URL in your `config.json` if you want log forwarding.",
62+
webhookName);
63+
return Optional.empty();
64+
}
65+
5866
try {
5967
return Optional.of(URI.create(webhookUri));
6068
} catch (IllegalArgumentException e) {
6169
logger.warn(LogMarkers.NO_DISCORD,
62-
"The webhook URL ({}) in the config is invalid, logs will not be forwarded to Discord.",
63-
webhookUri, e);
70+
"The {} webhook URL ({}) in the config is invalid, logs will not be forwarded to Discord.",
71+
webhookName, webhookUri, e);
6472
return Optional.empty();
6573
}
6674
}

0 commit comments

Comments
 (0)