From 968037478801709042497c409f06aed6c70949b0 Mon Sep 17 00:00:00 2001 From: bowser0000 Date: Sat, 29 Aug 2020 16:06:45 -0400 Subject: Add better Hypixel API error messages --- src/main/java/me/Danker/handlers/APIHandler.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/main/java/me/Danker/handlers/APIHandler.java b/src/main/java/me/Danker/handlers/APIHandler.java index 783ca92..34a73ff 100644 --- a/src/main/java/me/Danker/handlers/APIHandler.java +++ b/src/main/java/me/Danker/handlers/APIHandler.java @@ -2,10 +2,12 @@ package me.Danker.handlers; import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; +import java.util.Scanner; import com.google.gson.Gson; import com.google.gson.JsonArray; @@ -41,6 +43,17 @@ public class APIHandler { return object; } else { + if (urlString.startsWith("https://api.hypixel.net/")) { + InputStream errorStream = conn.getErrorStream(); + try (Scanner scanner = new Scanner(errorStream)) { + scanner.useDelimiter("\\Z"); + String error = scanner.next(); + + Gson gson = new Gson(); + JsonObject object = gson.fromJson(error, JsonObject.class); + return object; + } + } player.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "Request failed. HTTP Error Code: " + conn.getResponseCode())); } } catch (MalformedURLException ex) { -- cgit