Skip to content

Commit

Permalink
Catch JsonParseException in alot more places
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm516 committed Jun 23, 2024
1 parent 926b49d commit 95a48fb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rtm516.mcxboxbroadcast.core;

import com.google.gson.JsonParseException;
import com.rtm516.mcxboxbroadcast.core.configs.FriendSyncConfig;
import com.rtm516.mcxboxbroadcast.core.exceptions.XboxFriendsException;
import com.rtm516.mcxboxbroadcast.core.models.FriendModifyResponse;
Expand Down Expand Up @@ -76,7 +77,7 @@ public List<FollowerResponse.Person> get(boolean includeFollowing, boolean inclu
}
}
}
} catch (IOException | InterruptedException e) {
} catch (JsonParseException | IOException | InterruptedException e) {
logger.debug("Follower request response: " + lastResponse);
throw new XboxFriendsException(e.getMessage());
}
Expand Down Expand Up @@ -108,7 +109,7 @@ public List<FollowerResponse.Person> get(boolean includeFollowing, boolean inclu
}
}
}
} catch (IOException | InterruptedException e) {
} catch (JsonParseException | IOException | InterruptedException e) {
logger.debug("Social request response: " + lastResponse);
throw new XboxFriendsException(e.getMessage());
}
Expand Down Expand Up @@ -168,7 +169,7 @@ public boolean addIfRequired(String xuid, String gamertag) {
if (modifyResponse.isFollowingCaller() && modifyResponse.isFollowedByCaller()) {
return false;
}
} catch (InterruptedException | IOException e) {
} catch (JsonParseException | InterruptedException | IOException e) {
// Debug log it failed and assume we aren't friends
logger.debug("Failed to check if " + gamertag + " (" + xuid + ") is a friend: " + e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rtm516.mcxboxbroadcast.core;

import com.google.gson.JsonParseException;
import com.google.gson.stream.JsonReader;
import com.rtm516.mcxboxbroadcast.core.configs.FriendSyncConfig;
import com.rtm516.mcxboxbroadcast.core.exceptions.SessionCreationException;
Expand Down Expand Up @@ -133,13 +134,17 @@ protected void updateSession() throws SessionUpdateException {
checkConnection();

String responseBody = super.updateSessionInternal(Constants.CREATE_SESSION.formatted(this.sessionInfo.getSessionId()), new CreateSessionRequest(this.sessionInfo));
CreateSessionResponse sessionResponse = Constants.GSON.fromJson(responseBody, CreateSessionResponse.class);
try {
CreateSessionResponse sessionResponse = Constants.GSON.fromJson(responseBody, CreateSessionResponse.class);

// Restart if we have 28/30 session members
int players = sessionResponse.members().size();
if (players >= 28) {
logger.info("Restarting session due to " + players + "/30 players");
restart();
// Restart if we have 28/30 session members
int players = sessionResponse.members().size();
if (players >= 28) {
logger.info("Restarting session due to " + players + "/30 players");
restart();
}
} catch (JsonParseException e) {
throw new SessionUpdateException("Failed to parse session response: " + e.getMessage());
}
}

Expand Down Expand Up @@ -217,7 +222,7 @@ public void addSubSession(String id) {
// Update the list of sub-sessions
try {
Files.writeString(Paths.get(cache, "sub_sessions.json"), Constants.GSON.toJson(subSessionManagers.keySet()));
} catch (IOException e) {
} catch (JsonParseException | IOException e) {
coreLogger.error("Failed to update sub-session list", e);
}
}
Expand Down Expand Up @@ -250,7 +255,7 @@ public void removeSubSession(String id) {
// Update the list of sub-sessions
try {
Files.writeString(Paths.get(cache, "sub_sessions.json"), Constants.GSON.toJson(subSessionManagers.keySet()));
} catch (IOException e) {
} catch (JsonParseException | IOException e) {
coreLogger.error("Failed to update sub-session list", e);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.rtm516.mcxboxbroadcast.core;

import com.github.mizosoft.methanol.Methanol;
import com.google.gson.JsonParseException;
import com.rtm516.mcxboxbroadcast.core.exceptions.SessionCreationException;
import com.rtm516.mcxboxbroadcast.core.exceptions.SessionUpdateException;
import com.rtm516.mcxboxbroadcast.core.models.session.CreateHandleRequest;
Expand Down Expand Up @@ -200,13 +201,18 @@ private void createSession() throws SessionCreationException, SessionUpdateExcep
);

// Make the request to create the session handle
HttpRequest createHandleRequest = HttpRequest.newBuilder()
.uri(Constants.CREATE_HANDLE)
.header("Content-Type", "application/json")
.header("Authorization", token)
.header("x-xbl-contract-version", "107")
.POST(HttpRequest.BodyPublishers.ofString(Constants.GSON.toJson(createHandleContent)))
.build();
HttpRequest createHandleRequest;
try {
createHandleRequest = HttpRequest.newBuilder()
.uri(Constants.CREATE_HANDLE)
.header("Content-Type", "application/json")
.header("Authorization", token)
.header("x-xbl-contract-version", "107")
.POST(HttpRequest.BodyPublishers.ofString(Constants.GSON.toJson(createHandleContent)))
.build();
} catch (JsonParseException e) {
throw new SessionCreationException("Unable to create session handle, error parsing json: " + e.getMessage());
}

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

Expand Down Expand Up @@ -245,13 +251,18 @@ private void createSession() throws SessionCreationException, SessionUpdateExcep
* @throws SessionUpdateException If the update fails
*/
protected String updateSessionInternal(String url, Object data) throws SessionUpdateException {
HttpRequest createSessionRequest = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Content-Type", "application/json")
.header("Authorization", getTokenHeader())
.header("x-xbl-contract-version", "107")
.PUT(HttpRequest.BodyPublishers.ofString(Constants.GSON.toJson(data)))
.build();
HttpRequest createSessionRequest;
try {
createSessionRequest = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Content-Type", "application/json")
.header("Authorization", getTokenHeader())
.header("x-xbl-contract-version", "107")
.PUT(HttpRequest.BodyPublishers.ofString(Constants.GSON.toJson(data)))
.build();
} catch (JsonParseException e) {
throw new SessionUpdateException("Unable to update session information, error parsing json: " + e.getMessage());
}

HttpResponse<String> createSessionResponse;
try {
Expand Down Expand Up @@ -382,7 +393,7 @@ public SocialSummaryResponse socialSummary() {

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

Expand Down

0 comments on commit 95a48fb

Please sign in to comment.