Skip to content

Commit

Permalink
Merge pull request #26 from Dinty1/develop
Browse files Browse the repository at this point in the history
Extra exception handling to catch interrupted errors
  • Loading branch information
Scarsz authored Jul 31, 2024
2 parents ab1464b + 3fc74e1 commit f446284
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 39 deletions.
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

0 comments on commit f446284

Please sign in to comment.