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

Add exception handling to Http #4749

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ public void build(LiteralArgumentBuilder<CommandSource> builder) {
PlayerListEntry lookUpTarget = PlayerListEntryArgumentType.get(context);
UUID uuid = lookUpTarget.getProfile().getId();

NameHistory history = Http.get("https://laby.net/api/v2/user/" + uuid + "/get-profile").sendJson(NameHistory.class);
NameHistory history = Http.get("https://laby.net/api/v2/user/" + uuid + "/get-profile")
.exceptionHandler(e -> error("There was an error fetching that users name history."))
.sendJson(NameHistory.class);

if (history == null || history.username_history == null || history.username_history.length == 0) {
error("There was an error fetching that users name history.");
if (history == null) {
return;
} else if (history.username_history == null || history.username_history.length == 0) {
error("There was an error fetching that users name history.");
}

String name = lookUpTarget.getProfile().getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ private void onRenderIdkDude(DrawContext context, int mouseX, int mouseY, float
MeteorClient.LOG.info("Checking latest version of Meteor Client");

MeteorExecutor.execute(() -> {
String res = Http.get("https://meteorclient.com/api/stats").sendString();
String res = Http.get("https://meteorclient.com/api/stats")
.exceptionHandler(e -> MeteorClient.LOG.error("Could not fetch version information."))
.sendString();
if (res == null) return;

Version latestVer = new Version(JsonParser.parseString(res).getAsJsonObject().get("version").getAsString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,19 @@ public static void init() {

MeteorExecutor.execute(() -> {
// Cape owners
Stream<String> lines = Http.get(CAPE_OWNERS_URL).sendLines();
if (lines != null) lines.forEach(s -> {
String[] split = s.split(" ");

if (split.length >= 2) {
OWNERS.put(UUID.fromString(split[0]), split[1]);
if (!TEXTURES.containsKey(split[1])) TEXTURES.put(split[1], new Cape(split[1]));
}
});
Stream<String> lines = Http.get(CAPE_OWNERS_URL)
.exceptionHandler(e -> MeteorClient.LOG.error("Could not load capes: " + e.getMessage()))
.sendLines();
if (lines != null) {
lines.forEach(s -> {
String[] split = s.split(" ");

if (split.length >= 2) {
OWNERS.put(UUID.fromString(split[0]), split[1]);
if (!TEXTURES.containsKey(split[1])) TEXTURES.put(split[1], new Cape(split[1]));
}
});
} else return;

// Capes
lines = Http.get(CAPES_URL).sendLines();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*
* @author Crosby
*/
public record FailedHttpResponse<T>(HttpRequest request) implements HttpResponse<T> {
public record FailedHttpResponse<T>(HttpRequest request, Exception exception) implements HttpResponse<T> {
@Override
public int statusCode() {
return Http.BAD_REQUEST;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Date;
import java.util.function.Consumer;
import java.util.stream.Stream;

public class Http {
Expand All @@ -42,6 +43,7 @@ private enum Method {
public static class Request {
private final HttpRequest.Builder builder;
private Method method;
private Consumer<Exception> exceptionHandler = Exception::printStackTrace;

private Request(Method method, String url) {
try {
Expand Down Expand Up @@ -96,6 +98,16 @@ public Request bodyJson(Object object) {
return this;
}

public Request ignoreExceptions() {
exceptionHandler = e -> {};
return this;
}

public Request exceptionHandler(Consumer<Exception> exceptionHandler) {
this.exceptionHandler = exceptionHandler;
return this;
}

private <T> HttpResponse<T> _sendResponse(String accept, HttpResponse.BodyHandler<T> responseBodyHandler) {
builder.header("Accept", accept);
if (method != null) builder.method(method.name(), HttpRequest.BodyPublishers.noBody());
Expand All @@ -105,8 +117,8 @@ private <T> HttpResponse<T> _sendResponse(String accept, HttpResponse.BodyHandle
try {
return CLIENT.send(request, responseBodyHandler);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
return new FailedHttpResponse<>(request);
exceptionHandler.accept(e);
return new FailedHttpResponse<>(request, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public static void update() {
long time = System.currentTimeMillis();

if (time - lastPingTime > 5 * 60 * 1000) {
MeteorExecutor.execute(() -> Http.post("https://meteorclient.com/api/online/ping").send());
MeteorExecutor.execute(() -> Http.post("https://meteorclient.com/api/online/ping").ignoreExceptions().send());

lastPingTime = time;
}
}

public static void leave() {
MeteorExecutor.execute(() -> Http.post("https://meteorclient.com/api/online/leave").send());
MeteorExecutor.execute(() -> Http.post("https://meteorclient.com/api/online/leave").ignoreExceptions().send());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ private static void init() {

GithubRepo repo = credit.addon.getRepo();
Http.Request request = Http.get("https://api.github.com/repos/%s/branches/%s".formatted(repo.getOwnerName(), repo.branch()));
request.exceptionHandler(e -> MeteorClient.LOG.error("Could not fetch repository information for addon '%s'.".formatted(credit.addon.name), e));
repo.authenticate(request);
HttpResponse<Response> res = request.sendJsonResponse(Response.class);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package meteordevelopment.meteorclient.utils.render;

import com.google.gson.Gson;
import meteordevelopment.meteorclient.MeteorClient;
import meteordevelopment.meteorclient.systems.accounts.TexturesJson;
import meteordevelopment.meteorclient.systems.accounts.UuidToProfileResponse;
import meteordevelopment.meteorclient.utils.PostInit;
Expand Down Expand Up @@ -28,7 +29,9 @@ public static PlayerHeadTexture fetchHead(UUID id) {
}

public static String getSkinUrl(UUID id) {
UuidToProfileResponse res2 = Http.get("https://sessionserver.mojang.com/session/minecraft/profile/" + id).sendJson(UuidToProfileResponse.class);
UuidToProfileResponse res2 = Http.get("https://sessionserver.mojang.com/session/minecraft/profile/" + id)
.exceptionHandler(e -> MeteorClient.LOG.error("Could not contact mojang session servers.", e))
.sendJson(UuidToProfileResponse.class);
if (res2 == null) return null;

String base64Textures = res2.getPropertyValue("textures");
Expand Down
Loading