Skip to content

Commit

Permalink
Forward shutdown reason to awaitStatus exception (#2268)
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Nov 2, 2022
1 parent 525faae commit 040f0ab
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/main/java/net/dv8tion/jda/internal/JDAImpl.java
Expand Up @@ -76,10 +76,7 @@
import net.dv8tion.jda.internal.requests.restaction.CommandEditActionImpl;
import net.dv8tion.jda.internal.requests.restaction.CommandListUpdateActionImpl;
import net.dv8tion.jda.internal.requests.restaction.GuildActionImpl;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.Helpers;
import net.dv8tion.jda.internal.utils.JDALogger;
import net.dv8tion.jda.internal.utils.UnlockHook;
import net.dv8tion.jda.internal.utils.*;
import net.dv8tion.jda.internal.utils.cache.AbstractCacheView;
import net.dv8tion.jda.internal.utils.cache.SnowflakeCacheViewImpl;
import net.dv8tion.jda.internal.utils.config.AuthorizationConfig;
Expand Down Expand Up @@ -130,6 +127,7 @@ public class JDAImpl implements JDA
protected final SessionConfig sessionConfig;
protected final MetaConfig metaConfig;

public ShutdownReason shutdownReason = ShutdownReason.USER_SHUTDOWN; // indicates why shutdown happened in awaitStatus / awaitReady
protected WebSocketClient client;
protected Requester requester;
protected IAudioSendFactory audioSendFactory = new DefaultSendFactory();
Expand Down Expand Up @@ -490,7 +488,7 @@ public JDA awaitStatus(@Nonnull Status status, @Nonnull Status... failOn) throws
|| getStatus().ordinal() < status.ordinal()) // Wait until status is bypassed
{
if (getStatus() == Status.SHUTDOWN)
throw new IllegalStateException("Was shutdown trying to await status");
throw new IllegalStateException("Was shutdown trying to await status.\nReason: " + shutdownReason);
else if (failStatus.contains(getStatus()))
return this;
Thread.sleep(50);
Expand Down
Expand Up @@ -45,6 +45,7 @@
import net.dv8tion.jda.internal.managers.PresenceImpl;
import net.dv8tion.jda.internal.utils.IOUtil;
import net.dv8tion.jda.internal.utils.JDALogger;
import net.dv8tion.jda.internal.utils.ShutdownReason;
import net.dv8tion.jda.internal.utils.UnlockHook;
import net.dv8tion.jda.internal.utils.cache.AbstractCacheView;
import net.dv8tion.jda.internal.utils.compress.Decompressor;
Expand Down Expand Up @@ -499,10 +500,28 @@ else if (clientCloseFrame != null)
//or that a bot reached a new shard minimum and cannot connect with the current settings
//if that is the case we have to drop our connection and inform the user with a fatal error message
LOG.error("WebSocket connection was closed and cannot be recovered due to identification issues\n{}", closeCode);

// Forward the close reason to any hooks to awaitStatus / awaitReady
// Since people cannot read logs, we have to explicitly forward this error.
switch (closeCode)
{
case SHARDING_REQUIRED:
case INVALID_SHARD:
api.shutdownReason = ShutdownReason.INVALID_SHARDS;
break;
case DISALLOWED_INTENTS:
api.shutdownReason = ShutdownReason.DISALLOWED_INTENTS;
break;
case GRACEFUL_CLOSE:
break;
default:
api.shutdownReason = new ShutdownReason("Connection closed with code " + closeCode);
}
}

if (decompressor != null)
decompressor.shutdown();

api.shutdownInternals();
api.handleEvent(new ShutdownEvent(api, OffsetDateTime.now(), rawCloseCode));
}
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/net/dv8tion/jda/internal/utils/ShutdownReason.java
@@ -0,0 +1,43 @@
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dv8tion.jda.internal.utils;

public class ShutdownReason
{
public static final ShutdownReason USER_SHUTDOWN = new ShutdownReason("User requested shutdown");
public static final ShutdownReason INVALID_SHARDS = new ShutdownReason("Invalid shard configuration");
public static final ShutdownReason DISALLOWED_INTENTS = new ShutdownReason("You tried turning on an intent you aren't allowed to use. " +
"For more information check https://jda.wiki/using-jda/troubleshooting/#im-getting-closecode4014-disallowed-intents");

protected final String reason;

public ShutdownReason(String reason)
{
this.reason = reason;
}

public String getReason()
{
return reason;
}

@Override
public String toString()
{
return reason;
}
}

0 comments on commit 040f0ab

Please sign in to comment.