| 1 | package com.mojang.realmsclient.client;
|
| 2 |
|
| 3 | import com.google.common.base.Strings;
|
| 4 | import com.google.gson.JsonObject;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import com.mojang.realmsclient.exception.RealmsHttpException;
|
| 7 | import java.util.Locale;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.locale.Language;
|
| 11 | import net.minecraft.network.chat.Component;
|
| 12 | import net.minecraft.util.GsonHelper;
|
| 13 | import net.minecraft.util.LenientJsonParser;
|
| 14 | import org.jspecify.annotations.Nullable;
|
| 15 | import org.slf4j.Logger;
|
| 16 |
|
| 17 | @Environment(EnvType.CLIENT)
|
| 18 | public interface RealmsError {
|
| 19 | Component NO_MESSAGE = Component.translatable("mco.errorMessage.noDetails");
|
| 20 | Logger LOGGER = LogUtils.getLogger();
|
| 21 |
|
| 22 | int errorCode();
|
| 23 |
|
| 24 | Component errorMessage();
|
| 25 |
|
| 26 | String logMessage();
|
| 27 |
|
| 28 | static RealmsError parse(final int httpCode, final String payload) {
|
| 29 | if (httpCode == 429) {
|
| 30 | return RealmsError.CustomError.SERVICE_BUSY;
|
| 31 | }
|
| 32 |
|
| 33 | if (Strings.isNullOrEmpty(payload)) {
|
| 34 | return RealmsError.CustomError.noPayload(httpCode);
|
| 35 | }
|
| 36 |
|
| 37 | try {
|
| 38 | JsonObject object = LenientJsonParser.parse(payload).getAsJsonObject();
|
| 39 | String errorReason = GsonHelper.getAsString(object, "reason", null);
|
| 40 | String errorMessage = GsonHelper.getAsString(object, "errorMsg", null);
|
| 41 | int errorCode = GsonHelper.getAsInt(object, "errorCode", -1);
|
| 42 | if (errorMessage != null || errorReason != null || errorCode != -1) {
|
| 43 | return new RealmsError.ErrorWithJsonPayload(httpCode, errorCode != -1 ? errorCode : httpCode, errorReason, errorMessage);
|
| 44 | }
|
| 45 | } catch (Exception e) {
|
| 46 | LOGGER.error("Could not parse RealmsError", e);
|
| 47 | }
|
| 48 |
|
| 49 | return new RealmsError.ErrorWithRawPayload(httpCode, payload);
|
| 50 | }
|
| 51 |
|
| 52 | @Environment(EnvType.CLIENT)
|
| 53 | record AuthenticationError(String message) implements RealmsError {
|
| 54 | public static final int ERROR_CODE = 401;
|
| 55 |
|
| 56 | @Override
|
| 57 | public int errorCode() {
|
| 58 | return 401;
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | public Component errorMessage() {
|
| 63 | return Component.literal(this.message);
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | public String logMessage() {
|
| 68 | return String.format(Locale.ROOT, "Realms authentication error with message '%s'", this.message);
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | @Environment(EnvType.CLIENT)
|
| 73 | record CustomError(int httpCode, @Nullable Component payload) implements RealmsError {
|
| 74 | public static final RealmsError.CustomError SERVICE_BUSY = new RealmsError.CustomError(429, Component.translatable("mco.errorMessage.serviceBusy"));
|
| 75 | public static final Component RETRY_MESSAGE = Component.translatable("mco.errorMessage.retry");
|
| 76 | public static final String BODY_TAG = "<body>";
|
| 77 | public static final String CLOSING_BODY_TAG = "</body>";
|
| 78 |
|
| 79 | public static RealmsError.CustomError unknownCompatibilityResponse(final String response) {
|
| 80 | return new RealmsError.CustomError(500, Component.translatable("mco.errorMessage.realmsService.unknownCompatibility", response));
|
| 81 | }
|
| 82 |
|
| 83 | public static RealmsError.CustomError configurationError() {
|
| 84 | return new RealmsError.CustomError(500, Component.translatable("mco.errorMessage.realmsService.configurationError"));
|
| 85 | }
|
| 86 |
|
| 87 | public static RealmsError.CustomError connectivityError(final RealmsHttpException exception) {
|
| 88 | return new RealmsError.CustomError(500, Component.translatable("mco.errorMessage.realmsService.connectivity", exception.getMessage()));
|
| 89 | }
|
| 90 |
|
| 91 | public static RealmsError.CustomError retry(final int statusCode) {
|
| 92 | return new RealmsError.CustomError(statusCode, RETRY_MESSAGE);
|
| 93 | }
|
| 94 |
|
| 95 | public static RealmsError.CustomError noPayload(final int statusCode) {
|
| 96 | return new RealmsError.CustomError(statusCode, null);
|
| 97 | }
|
| 98 |
|
| 99 | public static RealmsError.CustomError htmlPayload(final int statusCode, final String payload) {
|
| 100 | int bodyStart = payload.indexOf("<body>");
|
| 101 | int bodyEnd = payload.indexOf("</body>");
|
| 102 | if (bodyStart >= 0 && bodyEnd > bodyStart) {
|
| 103 | return new RealmsError.CustomError(statusCode, Component.literal(payload.substring(bodyStart + "<body>".length(), bodyEnd).trim()));
|
| 104 | }
|
| 105 |
|
| 106 | LOGGER.error("Got an error with an unreadable html body {}", payload);
|
| 107 | return new RealmsError.CustomError(statusCode, null);
|
| 108 | }
|
| 109 |
|
| 110 | @Override
|
| 111 | public int errorCode() {
|
| 112 | return this.httpCode;
|
| 113 | }
|
| 114 |
|
| 115 | @Override
|
| 116 | public Component errorMessage() {
|
| 117 | return this.payload != null ? this.payload : NO_MESSAGE;
|
| 118 | }
|
| 119 |
|
| 120 | @Override
|
| 121 | public String logMessage() {
|
| 122 | return this.payload != null
|
| 123 | ? String.format(Locale.ROOT, "Realms service error (%d) with message '%s'", this.httpCode, this.payload.getString())
|
| 124 | : String.format(Locale.ROOT, "Realms service error (%d) with no payload", this.httpCode);
|
| 125 | }
|
| 126 | }
|
| 127 |
|
| 128 | @Environment(EnvType.CLIENT)
|
| 129 | record ErrorWithJsonPayload(int httpCode, int code, @Nullable String reason, @Nullable String message) implements RealmsError {
|
| 130 | @Override
|
| 131 | public int errorCode() {
|
| 132 | return this.code;
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | public Component errorMessage() {
|
| 137 | String codeTranslationKey = "mco.errorMessage." + this.code;
|
| 138 | if (Language.getInstance().has(codeTranslationKey)) {
|
| 139 | return Component.translatable(codeTranslationKey);
|
| 140 | }
|
| 141 |
|
| 142 | if (this.reason != null) {
|
| 143 | String reasonTranslationKey = "mco.errorReason." + this.reason;
|
| 144 | if (Language.getInstance().has(reasonTranslationKey)) {
|
| 145 | return Component.translatable(reasonTranslationKey);
|
| 146 | }
|
| 147 | }
|
| 148 |
|
| 149 | return this.message != null ? Component.literal(this.message) : NO_MESSAGE;
|
| 150 | }
|
| 151 |
|
| 152 | @Override
|
| 153 | public String logMessage() {
|
| 154 | return String.format(Locale.ROOT, "Realms service error (%d/%d/%s) with message '%s'", this.httpCode, this.code, this.reason, this.message);
|
| 155 | }
|
| 156 | }
|
| 157 |
|
| 158 | @Environment(EnvType.CLIENT)
|
| 159 | record ErrorWithRawPayload(int httpCode, String payload) implements RealmsError {
|
| 160 | @Override
|
| 161 | public int errorCode() {
|
| 162 | return this.httpCode;
|
| 163 | }
|
| 164 |
|
| 165 | @Override
|
| 166 | public Component errorMessage() {
|
| 167 | return Component.literal(this.payload);
|
| 168 | }
|
| 169 |
|
| 170 | @Override
|
| 171 | public String logMessage() {
|
| 172 | return String.format(Locale.ROOT, "Realms service error (%d) with raw payload '%s'", this.httpCode, this.payload);
|
| 173 | }
|
| 174 | }
|
| 175 | }
|
| 176 |
|