Skip to content

Commit ee91899

Browse files
committed
handle http timeout exception more gracefully
1 parent 91d4593 commit ee91899

File tree

10 files changed

+147
-38
lines changed

10 files changed

+147
-38
lines changed

src/main/java/vc/commands/ChatSearchCommand.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import vc.openapi.handler.ChatsApi;
2222
import vc.openapi.model.ChatSearchResponse;
2323

24+
import java.net.http.HttpTimeoutException;
2425
import java.time.LocalDate;
2526
import java.util.Optional;
2627
import java.util.concurrent.atomic.AtomicBoolean;
@@ -71,11 +72,20 @@ public Mono<Message> resolve(DeferrableInteractionEvent event, String word, int
7172
try {
7273
response = chatsApi.chatSearch(word, startDate, endDate, 25, page);
7374
} catch (final Exception e) {
74-
if (e instanceof ApiException apiException
75-
&& (apiException.getCause() instanceof MismatchedInputException || apiException.getCode() == 204)) {
76-
// fall through
75+
if (e instanceof ApiException apiException) {
76+
if (apiException.getCause() instanceof MismatchedInputException || apiException.getCode() == 204) {
77+
return event.createFollowup()
78+
.withEmbeds(EmbedCreateSpec.builder()
79+
.color(Color.RUBY)
80+
.description("No chats containing this word were found. That's pretty rare!")
81+
.build());
82+
} else if (apiException.getCause() instanceof HttpTimeoutException httpTimeoutException) {
83+
LOGGER.error("Timeout searching for word: {}", word, httpTimeoutException);
84+
return error(event, "Timeout searching for word. Try again in a minute");
85+
}
7786
} else {
7887
LOGGER.error("Error searching for word: {}", word, e);
88+
throw new RuntimeException(e);
7989
}
8090
}
8191
if (response == null || response.getChats() == null || response.getChats().isEmpty())

src/main/java/vc/commands/ChatsCommand.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import vc.openapi.model.ChatsResponse;
2121
import vc.util.PlayerLookup;
2222

23+
import java.net.http.HttpTimeoutException;
2324
import java.time.LocalDate;
2425
import java.util.concurrent.atomic.AtomicBoolean;
2526

@@ -63,11 +64,21 @@ private Mono<Message> resolveChats(final DeferrableInteractionEvent event, final
6364
try {
6465
chatsResponse = chatsApi.chats(identity.uuid(), null, startDate, endDate, 25, page);
6566
} catch (final Exception e) {
66-
if (e instanceof ApiException apiException
67-
&& (apiException.getCause() instanceof MismatchedInputException || apiException.getCode() == 204)) {
68-
// fall through
67+
if (e instanceof ApiException apiException) {
68+
if (apiException.getCause() instanceof MismatchedInputException || apiException.getCode() == 204) {
69+
return event.createFollowup()
70+
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
71+
.color(Color.RUBY)
72+
.description("No chats found")
73+
.thumbnail(identity.getAvatarURL())
74+
.build());
75+
} else if (apiException.getCause() instanceof HttpTimeoutException httpTimeoutException) {
76+
LOGGER.error("Timeout searching for chats: {}", identity.uuid(), httpTimeoutException);
77+
return error(event, "Timeout searching for chats. Try again in a minute");
78+
}
6979
} else {
70-
LOGGER.error("Error processing chats response", e);
80+
LOGGER.error("Error searching for chats: {}", identity.uuid(), e);
81+
throw new RuntimeException(e);
7182
}
7283
}
7384
if (chatsResponse == null || chatsResponse.getChats() == null || chatsResponse.getChats().isEmpty())

src/main/java/vc/commands/ConnectionsCommand.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import vc.openapi.model.ConnectionsResponse;
2424
import vc.util.PlayerLookup;
2525

26+
import java.net.http.HttpTimeoutException;
2627
import java.time.LocalDate;
2728
import java.util.concurrent.atomic.AtomicBoolean;
2829

@@ -66,11 +67,21 @@ private Mono<Message> resolveConnections(final DeferrableInteractionEvent event,
6667
try {
6768
connectionsResponse = connectionsApi.connections(identity.uuid(), null, startDate, endDate, 25, page);
6869
} catch (final Exception e) {
69-
if (e instanceof ApiException apiException
70-
&& (apiException.getCause() instanceof MismatchedInputException || apiException.getCode() == 204)) {
71-
// fall through
70+
if (e instanceof ApiException apiException) {
71+
if (apiException.getCause() instanceof MismatchedInputException || apiException.getCode() == 204) {
72+
return event.createFollowup()
73+
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
74+
.color(Color.RUBY)
75+
.description("No connections found")
76+
.thumbnail(identity.getAvatarURL())
77+
.build());
78+
} else if (apiException.getCause() instanceof HttpTimeoutException httpTimeoutException) {
79+
LOGGER.error("Timeout searching for connections: {}", identity.uuid(), httpTimeoutException);
80+
return error(event, "Timeout searching for connections. Try again in a minute");
81+
}
7282
} else {
7383
LOGGER.error("Error processing connections response", e);
84+
throw new RuntimeException(e);
7485
}
7586
}
7687
if (connectionsResponse == null || connectionsResponse.getConnections() == null || connectionsResponse.getConnections().isEmpty())
@@ -80,7 +91,6 @@ private Mono<Message> resolveConnections(final DeferrableInteractionEvent event,
8091
.description("No connections found")
8192
.thumbnail(identity.getAvatarURL())
8293
.build());
83-
8494
final StringBuilder result = new StringBuilder();
8595
final AtomicBoolean truncated = new AtomicBoolean(false);
8696
connectionsResponse.getConnections().stream()

src/main/java/vc/commands/DataCommand.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import vc.util.PlayerLookup;
1818

1919
import java.io.ByteArrayInputStream;
20+
import java.net.http.HttpTimeoutException;
2021

2122
import static org.slf4j.LoggerFactory.getLogger;
2223

@@ -47,12 +48,22 @@ public Mono<Message> resolvePlayerDataDump(ChatInputInteractionEvent event, Prof
4748
String playerDataDump = null;
4849
try {
4950
playerDataDump = vcDataDumpApi.getPlayerDataDump(identity.uuid(), null);
50-
} catch (final Exception e){
51-
if (e instanceof ApiException apiException
52-
&& (apiException.getCause() instanceof MismatchedInputException || apiException.getCode() == 204)) {
53-
// fall through
51+
} catch (final Exception e) {
52+
if (e instanceof ApiException apiException) {
53+
if (apiException.getCause() instanceof MismatchedInputException || apiException.getCode() == 204) {
54+
return event.createFollowup()
55+
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
56+
.color(Color.RUBY)
57+
.description("No Data")
58+
.thumbnail(identity.getAvatarURL())
59+
.build());
60+
} else if (apiException.getCause() instanceof HttpTimeoutException httpTimeoutException) {
61+
LOGGER.error("Timeout searching for data dump: {}", identity.uuid(), httpTimeoutException);
62+
return error(event, "Timeout searching for data. Try again in a minute");
63+
}
5464
} else {
5565
LOGGER.error("Failed to get player data dump", e);
66+
throw new RuntimeException(e);
5667
}
5768
}
5869
if (playerDataDump == null)

src/main/java/vc/commands/DeathsCommand.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import vc.openapi.model.DeathsResponse;
2424
import vc.util.PlayerLookup;
2525

26+
import java.net.http.HttpTimeoutException;
2627
import java.time.LocalDate;
2728
import java.util.concurrent.atomic.AtomicBoolean;
2829

@@ -66,11 +67,21 @@ private Mono<Message> resolveDeaths(final DeferrableInteractionEvent event, fina
6667
try {
6768
deathsResponse = deathsApi.deaths(identity.uuid(), null, startDate, endDate, 25, page);
6869
} catch (final Exception e) {
69-
if (e instanceof ApiException apiException
70-
&& (apiException.getCause() instanceof MismatchedInputException || apiException.getCode() == 204)) {
71-
// fall through
70+
if (e instanceof ApiException apiException) {
71+
if (apiException.getCause() instanceof MismatchedInputException || apiException.getCode() == 204) {
72+
return event.createFollowup()
73+
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
74+
.color(Color.RUBY)
75+
.description("No deaths found")
76+
.thumbnail(identity.getAvatarURL())
77+
.build());
78+
} else if (apiException.getCause() instanceof HttpTimeoutException httpTimeoutException) {
79+
LOGGER.error("Timeout searching for deaths: {}", identity.uuid(), httpTimeoutException);
80+
return error(event, "Timeout searching for deaths. Try again in a minute");
81+
}
7282
} else {
7383
LOGGER.error("Failed to get deaths", e);
84+
throw new RuntimeException(e);
7485
}
7586
}
7687
if (deathsResponse == null || deathsResponse.getDeaths() == null || deathsResponse.getDeaths().isEmpty())

src/main/java/vc/commands/KillsCommand.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import vc.openapi.model.KillsResponse;
2424
import vc.util.PlayerLookup;
2525

26+
import java.net.http.HttpTimeoutException;
2627
import java.time.LocalDate;
2728
import java.util.concurrent.atomic.AtomicBoolean;
2829

@@ -65,11 +66,21 @@ private Mono<Message> resolveKills(final DeferrableInteractionEvent event, final
6566
try {
6667
killsResponse = deathsApi.kills(identity.uuid(), null, startDate, endDate, 25, page);
6768
} catch (final Exception e) {
68-
if (e instanceof ApiException apiException
69-
&& (apiException.getCause() instanceof MismatchedInputException || apiException.getCode() == 204)) {
70-
// fall through
69+
if (e instanceof ApiException apiException) {
70+
if (apiException.getCause() instanceof MismatchedInputException || apiException.getCode() == 204) {
71+
return event.createFollowup()
72+
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
73+
.color(Color.RUBY)
74+
.description("No kills found")
75+
.thumbnail(identity.getAvatarURL())
76+
.build());
77+
} else if (apiException.getCause() instanceof HttpTimeoutException httpTimeoutException) {
78+
LOGGER.error("Timeout searching for kills: {}", identity.uuid(), httpTimeoutException);
79+
return error(event, "Timeout searching for kills. Try again in a minute");
80+
}
7181
} else {
7282
LOGGER.error("Error resolving kills", e);
83+
throw new RuntimeException(e);
7384
}
7485
}
7586
if (killsResponse == null || killsResponse.getKills() == null || killsResponse.getKills().isEmpty())

src/main/java/vc/commands/PlayerStatsCommand.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import vc.openapi.model.PlayerStats;
1616
import vc.util.PlayerLookup;
1717

18+
import java.net.http.HttpTimeoutException;
19+
1820
import static discord4j.common.util.TimestampFormat.SHORT_DATE_TIME;
1921
import static org.slf4j.LoggerFactory.getLogger;
2022

@@ -44,11 +46,21 @@ public Mono<Message> handle(final ChatInputInteractionEvent event) {
4446
try {
4547
playerStats = statsApi.playerStats(identity.uuid(), null);
4648
} catch (final Exception e) {
47-
if (e instanceof ApiException apiException
48-
&& (apiException.getCause() instanceof MismatchedInputException || apiException.getCode() == 204)) {
49-
// fall through
49+
if (e instanceof ApiException apiException) {
50+
if (apiException.getCause() instanceof MismatchedInputException || apiException.getCode() == 204) {
51+
return event.createFollowup()
52+
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
53+
.color(Color.RUBY)
54+
.description("No Data")
55+
.thumbnail(identity.getAvatarURL())
56+
.build());
57+
} else if (apiException.getCause() instanceof HttpTimeoutException httpTimeoutException) {
58+
LOGGER.error("Timeout getting stats for: {}", identity.uuid(), httpTimeoutException);
59+
return error(event, "Timeout getting stats. Try again in a minute");
60+
}
5061
} else {
5162
LOGGER.error("Failed to get stats for player: {}", identity.uuid(), e);
63+
throw new RuntimeException(e);
5264
}
5365
}
5466
if (playerStats == null)

src/main/java/vc/commands/PlaytimeCommand.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import vc.openapi.model.PlaytimeResponse;
1717
import vc.util.PlayerLookup;
1818

19+
import java.net.http.HttpTimeoutException;
1920
import java.util.ArrayList;
2021
import java.util.List;
2122

@@ -51,11 +52,21 @@ private Mono<Message> resolvePlaytime(ChatInputInteractionEvent event, final Pro
5152
try {
5253
playtime = playtimeApi.playtime(identity.uuid(), null);
5354
} catch (final Exception e) {
54-
if (e instanceof ApiException apiException
55-
&& (apiException.getCause() instanceof MismatchedInputException || apiException.getCode() == 204)) {
56-
// fall through
55+
if (e instanceof ApiException apiException) {
56+
if (apiException.getCause() instanceof MismatchedInputException || apiException.getCode() == 204) {
57+
return event.createFollowup()
58+
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
59+
.color(Color.RUBY)
60+
.description("Never Played")
61+
.thumbnail(identity.getAvatarURL())
62+
.build());
63+
} else if (apiException.getCause() instanceof HttpTimeoutException httpTimeoutException) {
64+
LOGGER.error("Timeout searching for playtime: {}", identity.uuid(), httpTimeoutException);
65+
return error(event, "Timeout getting playtime. Try again in a minute");
66+
}
5767
} else {
5868
LOGGER.error("Failed to get playtime for player: {}", identity.uuid(), e);
69+
throw new RuntimeException(e);
5970
}
6071
}
6172
if (isNull(playtime))

src/main/java/vc/commands/SeenCommand.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import vc.util.PlayerLookup;
1818

1919
import javax.annotation.Nullable;
20+
import java.net.http.HttpTimeoutException;
2021
import java.time.OffsetDateTime;
2122
import java.util.UUID;
2223

@@ -54,11 +55,21 @@ private Mono<Message> resolveSeen(final ChatInputInteractionEvent event, final P
5455
try {
5556
seenResponse = seenApi.seen(uuid, null);
5657
} catch (final Exception e) {
57-
if (e instanceof ApiException apiException
58-
&& (apiException.getCause() instanceof MismatchedInputException || apiException.getCode() == 204)) {
59-
// fall through
58+
if (e instanceof ApiException apiException) {
59+
if (apiException.getCause() instanceof MismatchedInputException || apiException.getCode() == 204) {
60+
return event.createFollowup()
61+
.withEmbeds(populateIdentity(EmbedCreateSpec.builder(), identity)
62+
.color(Color.RUBY)
63+
.description("Never Seen")
64+
.thumbnail(identity.getAvatarURL())
65+
.build());
66+
} else if (apiException.getCause() instanceof HttpTimeoutException httpTimeoutException) {
67+
LOGGER.error("Timeout getting seen for: {}", identity.uuid(), httpTimeoutException);
68+
return error(event, "Timeout getting seen data. Try again in a minute");
69+
}
6070
} else {
6171
LOGGER.error("Failed to get seen for player: {}", uuid, e);
72+
throw new RuntimeException(e);
6273
}
6374
}
6475
if (isNull(seenResponse))
@@ -70,11 +81,11 @@ private Mono<Message> resolveSeen(final ChatInputInteractionEvent event, final P
7081
.build());
7182
return event.createFollowup()
7283
.withEmbeds(populateIdentity(EmbedCreateSpec.builder()
73-
.addField("First seen", getSeenString(seenResponse.getFirstSeen()), false)
74-
.addField("Last seen", getSeenString(seenResponse.getLastSeen()), false), identity)
75-
.color(Color.CYAN)
76-
.thumbnail(identity.getAvatarURL())
77-
.build());
84+
.addField("First seen", getSeenString(seenResponse.getFirstSeen()), false)
85+
.addField("Last seen", getSeenString(seenResponse.getLastSeen()), false), identity)
86+
.color(Color.CYAN)
87+
.thumbnail(identity.getAvatarURL())
88+
.build());
7889
}
7990

8091
private String getSeenString(@Nullable final OffsetDateTime seen) {

src/main/java/vc/commands/WordCountCommand.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import vc.openapi.handler.ApiException;
1212
import vc.openapi.handler.ChatsApi;
1313

14+
import java.net.http.HttpTimeoutException;
15+
1416
import static org.slf4j.LoggerFactory.getLogger;
1517

1618
@Component
@@ -42,11 +44,20 @@ public Mono<Message> handle(final ChatInputInteractionEvent event) {
4244
try {
4345
count = chatsApi.wordCount(word).getCount();
4446
} catch (final Exception e) {
45-
if (e instanceof ApiException apiException
46-
&& (apiException.getCause() instanceof MismatchedInputException || apiException.getCode() == 204)) {
47-
// fall through
47+
if (e instanceof ApiException apiException) {
48+
if (apiException.getCause() instanceof MismatchedInputException || apiException.getCode() == 204) {
49+
return event.createFollowup()
50+
.withEmbeds(EmbedCreateSpec.builder()
51+
.color(Color.RUBY)
52+
.description("No chats containing this word were found. That's pretty rare!")
53+
.build());
54+
} else if (apiException.getCause() instanceof HttpTimeoutException httpTimeoutException) {
55+
LOGGER.error("Timeout searching for word: {}", word, httpTimeoutException);
56+
return error(event, "Timeout searching for word. Try again in a minute");
57+
}
4858
} else {
4959
LOGGER.error("Error getting word count: {}", word, e);
60+
throw new RuntimeException(e);
5061
}
5162
}
5263
if (count == null) {

0 commit comments

Comments
 (0)