Better error handling for valhalla

This commit is contained in:
Matthew Messinger 2018-07-22 15:47:34 -04:00
parent c5fca4c977
commit 7af8fa50e1
4 changed files with 31 additions and 34 deletions

View file

@ -602,11 +602,7 @@ public class GuiSkins extends GuiScreen {
private void onUploadComplete(SkinUploadResponse response) { private void onUploadComplete(SkinUploadResponse response) {
LiteLoaderLogger.info("Upload completed with: %s", response); LiteLoaderLogger.info("Upload completed with: %s", response);
this.uploadingSkin = false; this.uploadingSkin = false;
if (!response.isSuccess()) { this.pendingRemoteSkinRefresh = true;
this.setUploadError(response.getMessage());
} else {
this.pendingRemoteSkinRefresh = true;
}
} }
static { static {

View file

@ -109,7 +109,9 @@ public class LegacySkinServer implements SkinServer {
if (response.startsWith("ERROR: ")) { if (response.startsWith("ERROR: ")) {
response = response.substring(7); response = response.substring(7);
} }
return new SkinUploadResponse(response.equalsIgnoreCase("OK"), response); if (response.equalsIgnoreCase("OK") || !response.endsWith("OK"))
throw new IOException(response);
return new SkinUploadResponse(response);
}, HDSkinManager.skinUploadExecutor); }, HDSkinManager.skinUploadExecutor);
} }

View file

@ -4,18 +4,12 @@ import com.google.common.base.MoreObjects;
public class SkinUploadResponse { public class SkinUploadResponse {
private final boolean success;
private final String message; private final String message;
public SkinUploadResponse(boolean success, String message) { public SkinUploadResponse(String message) {
this.success = success;
this.message = message; this.message = message;
} }
public boolean isSuccess() {
return success;
}
public String getMessage() { public String getMessage() {
return message; return message;
} }
@ -23,7 +17,6 @@ public class SkinUploadResponse {
@Override @Override
public String toString() { public String toString() {
return MoreObjects.toStringHelper(this) return MoreObjects.toStringHelper(this)
.add("success", success)
.add("message", message) .add("message", message)
.toString(); .toString();
} }

View file

@ -15,6 +15,7 @@ import net.minecraft.client.Minecraft;
import net.minecraft.util.Session; import net.minecraft.util.Session;
import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.http.HttpHeaders; import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair; import org.apache.http.NameValuePair;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
@ -29,7 +30,6 @@ import org.apache.http.message.BasicNameValuePair;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.Reader; import java.io.Reader;
import java.net.URI; import java.net.URI;
@ -46,7 +46,7 @@ public class ValhallaSkinServer implements SkinServer {
@Expose @Expose
private final String address; private final String address;
private final Gson gson = new GsonBuilder() private static final Gson gson = new GsonBuilder()
.registerTypeAdapter(UUID.class, new UUIDTypeAdapter()) .registerTypeAdapter(UUID.class, new UUIDTypeAdapter())
.create(); .create();
@ -64,7 +64,7 @@ public class ValhallaSkinServer implements SkinServer {
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
return Optional.of(readJson(response.getEntity().getContent(), MinecraftTexturesPayload.class)); return Optional.of(readJson(response, MinecraftTexturesPayload.class));
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -132,16 +132,13 @@ public class ValhallaSkinServer implements SkinServer {
private SkinUploadResponse upload(CloseableHttpClient client, HttpUriRequest request) throws IOException { private SkinUploadResponse upload(CloseableHttpClient client, HttpUriRequest request) throws IOException {
try (CloseableHttpResponse response = client.execute(request)) { try (CloseableHttpResponse response = client.execute(request)) {
int code = response.getStatusLine().getStatusCode(); return readJson(response, SkinUploadResponse.class);
JsonObject json = readJson(response.getEntity().getContent(), JsonObject.class);
return new SkinUploadResponse(code == HttpStatus.SC_OK, json.get("message").getAsString());
} }
} }
private void authorize(CloseableHttpClient client, Session session) throws IOException, AuthenticationException { private void authorize(CloseableHttpClient client, Session session) throws IOException, AuthenticationException {
if (accessToken != null) { if (this.accessToken != null) {
return; return;
} }
GameProfile profile = session.getProfile(); GameProfile profile = session.getProfile();
@ -160,11 +157,19 @@ public class ValhallaSkinServer implements SkinServer {
throw new IOException("UUID mismatch!"); // probably won't ever throw throw new IOException("UUID mismatch!"); // probably won't ever throw
} }
this.accessToken = response.accessToken; this.accessToken = response.accessToken;
} }
private <T> T readJson(InputStream in, Class<T> cl) throws IOException { private <T> T readJson(HttpResponse resp, Class<T> cl) throws IOException {
try (Reader r = new InputStreamReader(in)) { String type = resp.getEntity().getContentType().getValue();
String enc = resp.getEntity().getContentEncoding().getValue();
if (!"application/json".equals(type)) {
throw new IOException("Server returned a non-json response!");
}
try (Reader r = new InputStreamReader(resp.getEntity().getContent(), enc)) {
if (resp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
// TODO specific error handling
throw new IOException(gson.fromJson(r, JsonObject.class).get("message").getAsString());
}
return gson.fromJson(r, cl); return gson.fromJson(r, cl);
} }
} }
@ -174,7 +179,7 @@ public class ValhallaSkinServer implements SkinServer {
.setUri(getHandshakeURI()) .setUri(getHandshakeURI())
.addParameter("name", name) .addParameter("name", name)
.build())) { .build())) {
return readJson(resp.getEntity().getContent(), AuthHandshake.class); return readJson(resp, AuthHandshake.class);
} }
} }
@ -184,7 +189,7 @@ public class ValhallaSkinServer implements SkinServer {
.addParameter("name", name) .addParameter("name", name)
.addParameter("verifyToken", String.valueOf(verifyToken)) .addParameter("verifyToken", String.valueOf(verifyToken))
.build())) { .build())) {
return readJson(resp.getEntity().getContent(), AuthResponse.class); return readJson(resp, AuthResponse.class);
} }
} }
@ -215,17 +220,18 @@ public class ValhallaSkinServer implements SkinServer {
} }
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
static class AuthHandshake { private static class AuthHandshake {
boolean offline; private boolean offline;
String serverId; private String serverId;
long verifyToken; private long verifyToken;
} }
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
static class AuthResponse { private static class AuthResponse {
private String accessToken;
private UUID userId;
String accessToken;
UUID userId;
} }
} }