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

Extra exception handling to catch interrupted errors #26

Merged
merged 8 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<version>1.18.34</version>
<scope>provided</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.scarsz.jdaappender;

import java.io.InterruptedIOException;
import java.util.concurrent.ScheduledFuture;

public interface IChannelLoggingHandler {
Expand All @@ -12,4 +13,12 @@ public interface IChannelLoggingHandler {

ScheduledFuture<?> getScheduledFuture();

default boolean isInterruptedException(Exception e) {
Throwable ex = e;
while (ex != null) {
if (ex instanceof InterruptedIOException || ex instanceof InterruptedException) return true;
ex = ex.getCause();
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -239,37 +239,23 @@ private Message updateMessage() throws IllegalStateException {
if (currentMessage != null) {
try {
return currentMessage.editMessage(full).submit().get();
} catch (ExecutionException e) {
} catch (Exception e) {
if (this.isInterruptedException(e)) return currentMessage;
Throwable cause = e.getCause();
if (cause instanceof ErrorResponseException
&& ((ErrorResponseException) cause).getErrorResponse() == ErrorResponse.UNKNOWN_MESSAGE) {
currentMessage = null;
} else {
throw new RuntimeException(cause);
}
} catch (InterruptedException e) {
JDA.Status status = channel.getJDA().getStatus();
if (executor == null || executor.isShutdown() || status == JDA.Status.SHUTTING_DOWN || status == JDA.Status.SHUTDOWN) {
// ignored, no-op
return currentMessage;
} else {
throw new RuntimeException(e);
throw new RuntimeException(e.getCause());
}
}
}

try {
return channel.sendMessage(full).submit().get();
} catch (ExecutionException e) {
} catch (Exception e) {
if (this.isInterruptedException(e)) return currentMessage;
throw new RuntimeException(e);
} catch (InterruptedException e) {
JDA.Status status = channel.getJDA().getStatus();
if (executor == null || executor.isShutdown() || status == JDA.Status.SHUTTING_DOWN || status == JDA.Status.SHUTDOWN) {
// ignored, no-op
return currentMessage;
} else {
throw new RuntimeException(e);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,37 +239,23 @@ private Message updateMessage() throws IllegalStateException {
if (currentMessage != null) {
try {
return currentMessage.editMessage(full).submit().get();
} catch (ExecutionException e) {
} catch (Exception e) {
if (this.isInterruptedException(e)) return currentMessage;
Throwable cause = e.getCause();
if (cause instanceof ErrorResponseException
&& ((ErrorResponseException) cause).getErrorResponse() == ErrorResponse.UNKNOWN_MESSAGE) {
currentMessage = null;
} else {
throw new RuntimeException(cause);
}
} catch (InterruptedException e) {
JDA.Status status = channel.getJDA().getStatus();
if (executor == null || executor.isShutdown() || status == JDA.Status.SHUTTING_DOWN || status == JDA.Status.SHUTDOWN) {
// ignored, no-op
return currentMessage;
} else {
throw new RuntimeException(e);
}
}
}

try {
return channel.sendMessage(full).submit().get();
} catch (ExecutionException e) {
} catch (Exception e) {
if (this.isInterruptedException(e)) return currentMessage;
throw new RuntimeException(e);
} catch (InterruptedException e) {
JDA.Status status = channel.getJDA().getStatus();
if (executor == null || executor.isShutdown() || status == JDA.Status.SHUTTING_DOWN || status == JDA.Status.SHUTDOWN) {
// ignored, no-op
return currentMessage;
} else {
throw new RuntimeException(e);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<version>1.18.34</version>
<scope>provided</scope>
</dependency>

Expand Down