RealmsError.java

com.mojang.realmsclient.client.RealmsError

信息

  • 全限定名:com.mojang.realmsclient.client.RealmsError
  • 类型:public interface
  • 包:com.mojang.realmsclient.client
  • 源码路径:src/main/java/com/mojang/realmsclient/client/RealmsError.java
  • 起始行号:L18
  • 职责:

    TODO

字段/常量

  • NO_MESSAGE

    • 类型: Component
    • 修饰符: package-private
    • 源码定位: L19
    • 说明:

      TODO

  • LOGGER

    • 类型: Logger
    • 修饰符: package-private
    • 源码定位: L20
    • 说明:

      TODO

内部类/嵌套类型

  • com.mojang.realmsclient.client.RealmsError.AuthenticationError

    • 类型: record
    • 修饰符: public
    • 源码定位: L51
    • 说明:

      TODO

  • com.mojang.realmsclient.client.RealmsError.CustomError

    • 类型: record
    • 修饰符: public
    • 源码定位: L71
    • 说明:

      TODO

  • com.mojang.realmsclient.client.RealmsError.ErrorWithJsonPayload

    • 类型: record
    • 修饰符: public
    • 源码定位: L127
    • 说明:

      TODO

  • com.mojang.realmsclient.client.RealmsError.ErrorWithRawPayload

    • 类型: record
    • 修饰符: public
    • 源码定位: L157
    • 说明:

      TODO

构造器

方法

下面的方法块按源码顺序生成。

int errorCode() @ L22

  • 方法名:errorCode
  • 源码定位:L22
  • 返回类型:int
  • 修饰符:package-private

参数:

说明:

TODO

Component errorMessage() @ L24

  • 方法名:errorMessage
  • 源码定位:L24
  • 返回类型:Component
  • 修饰符:package-private

参数:

说明:

TODO

String logMessage() @ L26

  • 方法名:logMessage
  • 源码定位:L26
  • 返回类型:String
  • 修饰符:package-private

参数:

说明:

TODO

static RealmsError parse(int httpCode, String payload) @ L28

  • 方法名:parse
  • 源码定位:L28
  • 返回类型:RealmsError
  • 修饰符:static

参数:

  • httpCode: int
  • payload: String

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public interface RealmsError {
    Component NO_MESSAGE = Component.translatable("mco.errorMessage.noDetails");
    Logger LOGGER = LogUtils.getLogger();
 
    int errorCode();
 
    Component errorMessage();
 
    String logMessage();
 
    static RealmsError parse(int httpCode, String payload) {
        if (httpCode == 429) {
            return RealmsError.CustomError.SERVICE_BUSY;
        } else if (Strings.isNullOrEmpty(payload)) {
            return RealmsError.CustomError.noPayload(httpCode);
        } else {
            try {
                JsonObject object = LenientJsonParser.parse(payload).getAsJsonObject();
                String errorReason = GsonHelper.getAsString(object, "reason", null);
                String errorMessage = GsonHelper.getAsString(object, "errorMsg", null);
                int errorCode = GsonHelper.getAsInt(object, "errorCode", -1);
                if (errorMessage != null || errorReason != null || errorCode != -1) {
                    return new RealmsError.ErrorWithJsonPayload(httpCode, errorCode != -1 ? errorCode : httpCode, errorReason, errorMessage);
                }
            } catch (Exception var6) {
                LOGGER.error("Could not parse RealmsError", (Throwable)var6);
            }
 
            return new RealmsError.ErrorWithRawPayload(httpCode, payload);
        }
    }
 
    @OnlyIn(Dist.CLIENT)
    public record AuthenticationError(String message) implements RealmsError {
        public static final int ERROR_CODE = 401;
 
        @Override
        public int errorCode() {
            return 401;
        }
 
        @Override
        public Component errorMessage() {
            return Component.literal(this.message);
        }
 
        @Override
        public String logMessage() {
            return String.format(Locale.ROOT, "Realms authentication error with message '%s'", this.message);
        }
    }
 
    @OnlyIn(Dist.CLIENT)
    public record CustomError(int httpCode, @Nullable Component payload) implements RealmsError {
        public static final RealmsError.CustomError SERVICE_BUSY = new RealmsError.CustomError(429, Component.translatable("mco.errorMessage.serviceBusy"));
        public static final Component RETRY_MESSAGE = Component.translatable("mco.errorMessage.retry");
        public static final String BODY_TAG = "<body>";
        public static final String CLOSING_BODY_TAG = "</body>";
 
        public static RealmsError.CustomError unknownCompatibilityResponse(String response) {
            return new RealmsError.CustomError(500, Component.translatable("mco.errorMessage.realmsService.unknownCompatibility", response));
        }
 
        public static RealmsError.CustomError configurationError() {
            return new RealmsError.CustomError(500, Component.translatable("mco.errorMessage.realmsService.configurationError"));
        }
 
        public static RealmsError.CustomError connectivityError(RealmsHttpException exception) {
            return new RealmsError.CustomError(500, Component.translatable("mco.errorMessage.realmsService.connectivity", exception.getMessage()));
        }
 
        public static RealmsError.CustomError retry(int statusCode) {
            return new RealmsError.CustomError(statusCode, RETRY_MESSAGE);
        }
 
        public static RealmsError.CustomError noPayload(int statusCode) {
            return new RealmsError.CustomError(statusCode, null);
        }
 
        public static RealmsError.CustomError htmlPayload(int statusCode, String payload) {
            int bodyStart = payload.indexOf("<body>");
            int bodyEnd = payload.indexOf("</body>");
            if (bodyStart >= 0 && bodyEnd > bodyStart) {
                return new RealmsError.CustomError(statusCode, Component.literal(payload.substring(bodyStart + "<body>".length(), bodyEnd).trim()));
            } else {
                LOGGER.error("Got an error with an unreadable html body {}", payload);
                return new RealmsError.CustomError(statusCode, null);
            }
        }
 
        @Override
        public int errorCode() {
            return this.httpCode;
        }
 
        @Override
        public Component errorMessage() {
            return this.payload != null ? this.payload : NO_MESSAGE;
        }
 
        @Override
        public String logMessage() {
            return this.payload != null
                ? String.format(Locale.ROOT, "Realms service error (%d) with message '%s'", this.httpCode, this.payload.getString())
                : String.format(Locale.ROOT, "Realms service error (%d) with no payload", this.httpCode);
        }
    }
 
    @OnlyIn(Dist.CLIENT)
    public record ErrorWithJsonPayload(int httpCode, int code, @Nullable String reason, @Nullable String message) implements RealmsError {
        @Override
        public int errorCode() {
            return this.code;
        }
 
        @Override
        public Component errorMessage() {
            String codeTranslationKey = "mco.errorMessage." + this.code;
            if (I18n.exists(codeTranslationKey)) {
                return Component.translatable(codeTranslationKey);
            } else {
                if (this.reason != null) {
                    String reasonTranslationKey = "mco.errorReason." + this.reason;
                    if (I18n.exists(reasonTranslationKey)) {
                        return Component.translatable(reasonTranslationKey);
                    }
                }
 
                return (Component)(this.message != null ? Component.literal(this.message) : NO_MESSAGE);
            }
        }
 
        @Override
        public String logMessage() {
            return String.format(Locale.ROOT, "Realms service error (%d/%d/%s) with message '%s'", this.httpCode, this.code, this.reason, this.message);
        }
    }
 
    @OnlyIn(Dist.CLIENT)
    public record ErrorWithRawPayload(int httpCode, String payload) implements RealmsError {
        @Override
        public int errorCode() {
            return this.httpCode;
        }
 
        @Override
        public Component errorMessage() {
            return Component.literal(this.payload);
        }
 
        @Override
        public String logMessage() {
            return String.format(Locale.ROOT, "Realms service error (%d) with raw payload '%s'", this.httpCode, this.payload);
        }
    }
}

引用的其他类

  • I18n

    • 引用位置: 方法调用
    • 关联成员: I18n.exists()
  • Component

    • 引用位置: 字段/方法调用/返回值
    • 关联成员: Component.literal(), Component.translatable()
  • GsonHelper

    • 引用位置: 方法调用
    • 关联成员: GsonHelper.getAsInt(), GsonHelper.getAsString()
  • LenientJsonParser

    • 引用位置: 方法调用
    • 关联成员: LenientJsonParser.parse()