Skip to content

Commit 95a48fb

Browse files
committed
Catch JsonParseException in alot more places
1 parent 926b49d commit 95a48fb

3 files changed

Lines changed: 44 additions & 27 deletions

File tree

core/src/main/java/com/rtm516/mcxboxbroadcast/core/FriendManager.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.rtm516.mcxboxbroadcast.core;
22

3+
import com.google.gson.JsonParseException;
34
import com.rtm516.mcxboxbroadcast.core.configs.FriendSyncConfig;
45
import com.rtm516.mcxboxbroadcast.core.exceptions.XboxFriendsException;
56
import com.rtm516.mcxboxbroadcast.core.models.FriendModifyResponse;
@@ -76,7 +77,7 @@ public List<FollowerResponse.Person> get(boolean includeFollowing, boolean inclu
7677
}
7778
}
7879
}
79-
} catch (IOException | InterruptedException e) {
80+
} catch (JsonParseException | IOException | InterruptedException e) {
8081
logger.debug("Follower request response: " + lastResponse);
8182
throw new XboxFriendsException(e.getMessage());
8283
}
@@ -108,7 +109,7 @@ public List<FollowerResponse.Person> get(boolean includeFollowing, boolean inclu
108109
}
109110
}
110111
}
111-
} catch (IOException | InterruptedException e) {
112+
} catch (JsonParseException | IOException | InterruptedException e) {
112113
logger.debug("Social request response: " + lastResponse);
113114
throw new XboxFriendsException(e.getMessage());
114115
}
@@ -168,7 +169,7 @@ public boolean addIfRequired(String xuid, String gamertag) {
168169
if (modifyResponse.isFollowingCaller() && modifyResponse.isFollowedByCaller()) {
169170
return false;
170171
}
171-
} catch (InterruptedException | IOException e) {
172+
} catch (JsonParseException | InterruptedException | IOException e) {
172173
// Debug log it failed and assume we aren't friends
173174
logger.debug("Failed to check if " + gamertag + " (" + xuid + ") is a friend: " + e.getMessage());
174175
}

core/src/main/java/com/rtm516/mcxboxbroadcast/core/SessionManager.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.rtm516.mcxboxbroadcast.core;
22

3+
import com.google.gson.JsonParseException;
34
import com.google.gson.stream.JsonReader;
45
import com.rtm516.mcxboxbroadcast.core.configs.FriendSyncConfig;
56
import com.rtm516.mcxboxbroadcast.core.exceptions.SessionCreationException;
@@ -133,13 +134,17 @@ protected void updateSession() throws SessionUpdateException {
133134
checkConnection();
134135

135136
String responseBody = super.updateSessionInternal(Constants.CREATE_SESSION.formatted(this.sessionInfo.getSessionId()), new CreateSessionRequest(this.sessionInfo));
136-
CreateSessionResponse sessionResponse = Constants.GSON.fromJson(responseBody, CreateSessionResponse.class);
137+
try {
138+
CreateSessionResponse sessionResponse = Constants.GSON.fromJson(responseBody, CreateSessionResponse.class);
137139

138-
// Restart if we have 28/30 session members
139-
int players = sessionResponse.members().size();
140-
if (players >= 28) {
141-
logger.info("Restarting session due to " + players + "/30 players");
142-
restart();
140+
// Restart if we have 28/30 session members
141+
int players = sessionResponse.members().size();
142+
if (players >= 28) {
143+
logger.info("Restarting session due to " + players + "/30 players");
144+
restart();
145+
}
146+
} catch (JsonParseException e) {
147+
throw new SessionUpdateException("Failed to parse session response: " + e.getMessage());
143148
}
144149
}
145150

@@ -217,7 +222,7 @@ public void addSubSession(String id) {
217222
// Update the list of sub-sessions
218223
try {
219224
Files.writeString(Paths.get(cache, "sub_sessions.json"), Constants.GSON.toJson(subSessionManagers.keySet()));
220-
} catch (IOException e) {
225+
} catch (JsonParseException | IOException e) {
221226
coreLogger.error("Failed to update sub-session list", e);
222227
}
223228
}
@@ -250,7 +255,7 @@ public void removeSubSession(String id) {
250255
// Update the list of sub-sessions
251256
try {
252257
Files.writeString(Paths.get(cache, "sub_sessions.json"), Constants.GSON.toJson(subSessionManagers.keySet()));
253-
} catch (IOException e) {
258+
} catch (JsonParseException | IOException e) {
254259
coreLogger.error("Failed to update sub-session list", e);
255260
}
256261

core/src/main/java/com/rtm516/mcxboxbroadcast/core/SessionManagerCore.java

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.rtm516.mcxboxbroadcast.core;
22

33
import com.github.mizosoft.methanol.Methanol;
4+
import com.google.gson.JsonParseException;
45
import com.rtm516.mcxboxbroadcast.core.exceptions.SessionCreationException;
56
import com.rtm516.mcxboxbroadcast.core.exceptions.SessionUpdateException;
67
import com.rtm516.mcxboxbroadcast.core.models.session.CreateHandleRequest;
@@ -200,13 +201,18 @@ private void createSession() throws SessionCreationException, SessionUpdateExcep
200201
);
201202

202203
// Make the request to create the session handle
203-
HttpRequest createHandleRequest = HttpRequest.newBuilder()
204-
.uri(Constants.CREATE_HANDLE)
205-
.header("Content-Type", "application/json")
206-
.header("Authorization", token)
207-
.header("x-xbl-contract-version", "107")
208-
.POST(HttpRequest.BodyPublishers.ofString(Constants.GSON.toJson(createHandleContent)))
209-
.build();
204+
HttpRequest createHandleRequest;
205+
try {
206+
createHandleRequest = HttpRequest.newBuilder()
207+
.uri(Constants.CREATE_HANDLE)
208+
.header("Content-Type", "application/json")
209+
.header("Authorization", token)
210+
.header("x-xbl-contract-version", "107")
211+
.POST(HttpRequest.BodyPublishers.ofString(Constants.GSON.toJson(createHandleContent)))
212+
.build();
213+
} catch (JsonParseException e) {
214+
throw new SessionCreationException("Unable to create session handle, error parsing json: " + e.getMessage());
215+
}
210216

211217
// Read the handle response
212218
HttpResponse<String> createHandleResponse;
@@ -216,7 +222,7 @@ private void createSession() throws SessionCreationException, SessionUpdateExcep
216222
CreateHandleResponse parsedResponse = Constants.GSON.fromJson(createHandleResponse.body(), CreateHandleResponse.class);
217223
sessionInfo.setHandleId(parsedResponse.id());
218224
}
219-
} catch (IOException | InterruptedException e) {
225+
} catch (JsonParseException | IOException | InterruptedException e) {
220226
throw new SessionCreationException(e.getMessage());
221227
}
222228

@@ -245,13 +251,18 @@ private void createSession() throws SessionCreationException, SessionUpdateExcep
245251
* @throws SessionUpdateException If the update fails
246252
*/
247253
protected String updateSessionInternal(String url, Object data) throws SessionUpdateException {
248-
HttpRequest createSessionRequest = HttpRequest.newBuilder()
249-
.uri(URI.create(url))
250-
.header("Content-Type", "application/json")
251-
.header("Authorization", getTokenHeader())
252-
.header("x-xbl-contract-version", "107")
253-
.PUT(HttpRequest.BodyPublishers.ofString(Constants.GSON.toJson(data)))
254-
.build();
254+
HttpRequest createSessionRequest;
255+
try {
256+
createSessionRequest = HttpRequest.newBuilder()
257+
.uri(URI.create(url))
258+
.header("Content-Type", "application/json")
259+
.header("Authorization", getTokenHeader())
260+
.header("x-xbl-contract-version", "107")
261+
.PUT(HttpRequest.BodyPublishers.ofString(Constants.GSON.toJson(data)))
262+
.build();
263+
} catch (JsonParseException e) {
264+
throw new SessionUpdateException("Unable to update session information, error parsing json: " + e.getMessage());
265+
}
255266

256267
HttpResponse<String> createSessionResponse;
257268
try {
@@ -382,7 +393,7 @@ public SocialSummaryResponse socialSummary() {
382393

383394
try {
384395
return Constants.GSON.fromJson(httpClient.send(socialSummaryRequest, HttpResponse.BodyHandlers.ofString()).body(), SocialSummaryResponse.class);
385-
} catch (IOException | InterruptedException e) {
396+
} catch (JsonParseException | IOException | InterruptedException e) {
386397
logger.error("Unable to get current friend count", e);
387398
}
388399

0 commit comments

Comments
 (0)