mirror of
https://github.com/DiscordSRV/Ascension.git
synced 2025-02-17 01:51:32 +01:00
Implement ErrorCallbackContext
This commit is contained in:
parent
bf60a978eb
commit
97a8816edd
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* This file is part of the DiscordSRV API, licensed under the MIT License
|
||||
* Copyright (c) 2016-2022 Austin "Scarsz" Shapiro, Henri "Vankka" Schubin and DiscordSRV contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.discordsrv.api.discord.connection.jda.errorresponse;
|
||||
|
||||
import net.dv8tion.jda.api.requests.RestAction;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Helper class to specify extra context to log with error responses.
|
||||
*/
|
||||
public final class ErrorCallbackContext {
|
||||
|
||||
private ErrorCallbackContext() {}
|
||||
|
||||
/**
|
||||
* Creates a failure callback that specifies additional context.
|
||||
* @param context the context of the request
|
||||
* @return the failure callback
|
||||
*/
|
||||
@NotNull
|
||||
public static Consumer<? super Throwable> context(@NotNull String context) {
|
||||
return throwable -> RestAction.getDefaultFailure().accept(new Exception(context, throwable));
|
||||
}
|
||||
|
||||
public static class Exception extends java.lang.Exception {
|
||||
|
||||
public Exception(String context, Throwable ex) {
|
||||
super(context, ex);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -18,6 +18,7 @@
|
||||
|
||||
package com.discordsrv.common.channel;
|
||||
|
||||
import com.discordsrv.api.discord.connection.jda.errorresponse.ErrorCallbackContext;
|
||||
import com.discordsrv.common.DiscordSRV;
|
||||
import com.discordsrv.common.config.main.ChannelUpdaterConfig;
|
||||
import com.discordsrv.common.logging.NamedLogger;
|
||||
@ -107,7 +108,7 @@ public class ChannelUpdaterModule extends AbstractModule<DiscordSRV> {
|
||||
|
||||
manager.timeout(30, TimeUnit.SECONDS).queue(
|
||||
null,
|
||||
t -> discordSRV.discordConnectionManager().handleRequestFailure("Failed to update channel " + channel, t)
|
||||
ErrorCallbackContext.context("Failed to update channel " + channel)
|
||||
);
|
||||
} catch (Throwable t) {
|
||||
discordSRV.logger().error("Failed to update channel " + channel, t);
|
||||
|
@ -30,6 +30,7 @@ import com.discordsrv.api.discord.api.entity.guild.DiscordGuild;
|
||||
import com.discordsrv.api.discord.api.entity.guild.DiscordRole;
|
||||
import com.discordsrv.api.discord.api.exception.NotReadyException;
|
||||
import com.discordsrv.api.discord.api.exception.RestErrorResponseException;
|
||||
import com.discordsrv.api.discord.connection.jda.errorresponse.ErrorCallbackContext;
|
||||
import com.discordsrv.common.DiscordSRV;
|
||||
import com.discordsrv.common.config.main.channels.base.BaseChannelConfig;
|
||||
import com.discordsrv.common.config.main.channels.base.IChannelConfig;
|
||||
@ -159,9 +160,10 @@ public class DiscordAPIImpl implements DiscordAPI {
|
||||
|
||||
futures.add(future.handle((threadChannel, t) -> {
|
||||
if (t != null) {
|
||||
discordSRV.discordConnectionManager().handleRequestFailure(
|
||||
ErrorCallbackContext.context(
|
||||
"Failed to deliver message to thread \""
|
||||
+ threadConfig.threadName + "\" in channel " + channel, t);
|
||||
+ threadConfig.threadName + "\" in channel " + channel
|
||||
).accept(t);
|
||||
throw new RuntimeException(); // Just here to fail the future
|
||||
}
|
||||
|
||||
|
@ -20,6 +20,7 @@ package com.discordsrv.common.discord.connection.jda;
|
||||
|
||||
import com.discordsrv.api.discord.api.entity.DiscordUser;
|
||||
import com.discordsrv.api.discord.connection.DiscordConnectionDetails;
|
||||
import com.discordsrv.api.discord.connection.jda.errorresponse.ErrorCallbackContext;
|
||||
import com.discordsrv.api.event.bus.EventPriority;
|
||||
import com.discordsrv.api.event.bus.Subscribe;
|
||||
import com.discordsrv.api.event.events.lifecycle.DiscordSRVShuttingDownEvent;
|
||||
@ -77,7 +78,8 @@ public class JDAConnectionManager implements DiscordConnectionManager {
|
||||
}
|
||||
|
||||
private final DiscordSRV discordSRV;
|
||||
private final DefaultFailureCallback defaultFailureCallback;
|
||||
private final FailureCallback failureCallback;
|
||||
private Future<?> failureCallbackFuture;
|
||||
private ScheduledExecutorService gatewayPool;
|
||||
private ScheduledExecutorService rateLimitPool;
|
||||
|
||||
@ -95,10 +97,10 @@ public class JDAConnectionManager implements DiscordConnectionManager {
|
||||
|
||||
public JDAConnectionManager(DiscordSRV discordSRV) {
|
||||
this.discordSRV = discordSRV;
|
||||
this.defaultFailureCallback = new DefaultFailureCallback(new NamedLogger(discordSRV, "DISCORD_REQUESTS"));
|
||||
this.failureCallback = new FailureCallback(new NamedLogger(discordSRV, "DISCORD_REQUESTS"));
|
||||
|
||||
// Set default failure handling
|
||||
RestAction.setDefaultFailure(defaultFailureCallback);
|
||||
// Default failure callback
|
||||
RestAction.setDefaultFailure(failureCallback);
|
||||
|
||||
// Disable all mentions by default for safety
|
||||
AllowedMentions.setDefaultMentions(Collections.emptyList());
|
||||
@ -116,6 +118,15 @@ public class JDAConnectionManager implements DiscordConnectionManager {
|
||||
return detailsAccepted;
|
||||
}
|
||||
|
||||
private void checkDefaultFailureCallback() {
|
||||
Consumer<? super Throwable> defaultFailure = RestAction.getDefaultFailure();
|
||||
if (defaultFailure != failureCallback) {
|
||||
discordSRV.logger().error("RestAction DefaultFailure was set to " + defaultFailure.getClass().getName() + " (" + defaultFailure + ")");
|
||||
discordSRV.logger().error("This is unsupported, please specify your own error handling on individual requests instead.");
|
||||
RestAction.setDefaultFailure(failureCallback);
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onStatusChange(StatusChangeEvent event) {
|
||||
DiscordSRV.Status currentStatus = discordSRV.status();
|
||||
@ -229,6 +240,12 @@ public class JDAConnectionManager implements DiscordConnectionManager {
|
||||
5,
|
||||
new CountingThreadFactory(Scheduler.THREAD_NAME_PREFIX + "JDA RateLimit #%s")
|
||||
);
|
||||
this.failureCallbackFuture = discordSRV.scheduler().runAtFixedRate(
|
||||
this::checkDefaultFailureCallback,
|
||||
30,
|
||||
120,
|
||||
TimeUnit.SECONDS
|
||||
);
|
||||
|
||||
ConnectionConfig.Bot botConfig = discordSRV.connectionConfig().bot;
|
||||
DiscordConnectionDetails connectionDetails = discordSRV.discordConnectionDetails();
|
||||
@ -345,6 +362,9 @@ public class JDAConnectionManager implements DiscordConnectionManager {
|
||||
if (rateLimitPool != null && !rateLimitPool.isShutdown()) {
|
||||
rateLimitPool.shutdownNow();
|
||||
}
|
||||
if (failureCallbackFuture != null) {
|
||||
failureCallbackFuture.cancel(false);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
@ -440,21 +460,21 @@ public class JDAConnectionManager implements DiscordConnectionManager {
|
||||
discordSRV.logger().error("+------------------------------>");
|
||||
}
|
||||
|
||||
public void handleRequestFailure(String context, Throwable cause) {
|
||||
defaultFailureCallback.accept(context, cause);
|
||||
}
|
||||
|
||||
private class DefaultFailureCallback implements Consumer<Throwable> {
|
||||
private class FailureCallback implements Consumer<Throwable> {
|
||||
|
||||
private final Logger logger;
|
||||
|
||||
protected DefaultFailureCallback(Logger logger) {
|
||||
protected FailureCallback(Logger logger) {
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(Throwable t) {
|
||||
accept(null, t);
|
||||
if (t instanceof ErrorCallbackContext.Exception) {
|
||||
accept(t.getMessage(), t.getCause());
|
||||
} else {
|
||||
accept(null, t);
|
||||
}
|
||||
}
|
||||
|
||||
public void accept(String context, Throwable t) {
|
||||
@ -481,7 +501,7 @@ public class JDAConnectionManager implements DiscordConnectionManager {
|
||||
Throwable cause = exception.getCause();
|
||||
if (cause != null) {
|
||||
// Run the cause through this method again
|
||||
accept(cause);
|
||||
accept(context, cause);
|
||||
} else {
|
||||
logger.error((context != null ? context + ": " : "") + "Failed to complete request for a unknown reason", exception);
|
||||
}
|
||||
|
@ -18,6 +18,7 @@
|
||||
|
||||
package com.discordsrv.common.invite;
|
||||
|
||||
import com.discordsrv.api.discord.connection.jda.errorresponse.ErrorCallbackContext;
|
||||
import com.discordsrv.api.placeholder.FormattedText;
|
||||
import com.discordsrv.api.placeholder.annotation.Placeholder;
|
||||
import com.discordsrv.common.DiscordSRV;
|
||||
@ -88,7 +89,10 @@ public class DiscordInviteModule extends AbstractModule<DiscordSRV> {
|
||||
return;
|
||||
}
|
||||
|
||||
channel.createInvite().setMaxAge(0).setUnique(true).queue(inv -> this.invite = inv.getUrl());
|
||||
channel.createInvite().setMaxAge(0).setUnique(true).queue(
|
||||
inv -> this.invite = inv.getUrl(),
|
||||
ErrorCallbackContext.context("Failed to auto create invite")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -25,6 +25,7 @@ import com.discordsrv.api.discord.api.entity.channel.DiscordThreadChannel;
|
||||
import com.discordsrv.api.discord.api.entity.message.ReceivedDiscordMessage;
|
||||
import com.discordsrv.api.discord.api.entity.message.ReceivedDiscordMessageCluster;
|
||||
import com.discordsrv.api.discord.api.entity.message.SendableDiscordMessage;
|
||||
import com.discordsrv.api.discord.connection.jda.errorresponse.ErrorCallbackContext;
|
||||
import com.discordsrv.api.event.events.message.receive.game.AbstractGameMessageReceiveEvent;
|
||||
import com.discordsrv.api.placeholder.FormattedText;
|
||||
import com.discordsrv.api.player.DiscordSRVPlayer;
|
||||
@ -137,8 +138,7 @@ public abstract class AbstractGameMessageModule<T extends IMessageConfig, E exte
|
||||
if (t instanceof CompletionException) {
|
||||
t = t.getCause();
|
||||
}
|
||||
discordSRV.discordConnectionManager().handleRequestFailure(
|
||||
"Failed to deliver a message to " + entry.getValue(), t);
|
||||
ErrorCallbackContext.context("Failed to deliver a message to " + entry.getValue()).accept(t);
|
||||
return null;
|
||||
});
|
||||
// Ignore ones that failed
|
||||
|
Loading…
Reference in New Issue
Block a user