fix TimiX multilingual

This commit is contained in:
Timi
2025-11-07 16:54:56 +08:00
parent d706574879
commit 12bbc62ef0
3 changed files with 27 additions and 14 deletions

View File

@ -15,9 +15,9 @@ public class TimiError extends AssertionError implements LanguageMsgMapping<Timi
/** 代码 */ /** 代码 */
protected final TimiCode code; protected final TimiCode code;
protected String msgKey; protected transient String msgKey;
protected Map<String, Object> msgArgs; protected transient Map<String, Object> msgArgs;
/** @param code 代码 */ /** @param code 代码 */
public TimiError(TimiCode code) { public TimiError(TimiCode code) {
@ -45,7 +45,7 @@ public class TimiError extends AssertionError implements LanguageMsgMapping<Timi
} }
public TimiResponse<?> toResponse() { public TimiResponse<?> toResponse() {
return new TimiResponse<>(code).msg(getMessage()).msgKey(msgKey); return new TimiResponse<>(code).msg(getMessage()).msgKey(msgKey, msgArgs);
} }
@Override @Override

View File

@ -17,9 +17,9 @@ public class TimiException extends RuntimeException implements LanguageMsgMappin
/** 代码 */ /** 代码 */
protected final TimiCode code; protected final TimiCode code;
protected String msgKey; protected transient String msgKey;
protected Map<String, Object> msgArgs; protected transient Map<String, Object> msgArgs;
/** @param code 代码 */ /** @param code 代码 */
public TimiException(TimiCode code) { public TimiException(TimiCode code) {
@ -58,7 +58,7 @@ public class TimiException extends RuntimeException implements LanguageMsgMappin
} }
public TimiResponse<?> toResponse() { public TimiResponse<?> toResponse() {
return new TimiResponse<>(code).msg(getMessage()).msgKey(msgKey); return new TimiResponse<>(code).msg(getMessage()).msgKey(msgKey, msgArgs);
} }
@Override @Override
@ -116,12 +116,25 @@ public class TimiException extends RuntimeException implements LanguageMsgMappin
return t; return t;
} }
public static <T> T required(T t, String message, String msgKey) {
if (TimiJava.isEmpty(t)) {
throw new TimiException(TimiCode.ARG_MISS, message).msgKey(msgKey);
}
return t;
}
public static <T> void requiredNull(T t, String message) throws TimiException { public static <T> void requiredNull(T t, String message) throws TimiException {
if (t != null) { if (t != null) {
throw new TimiException(TimiCode.ERROR, message); throw new TimiException(TimiCode.ERROR, message);
} }
} }
public static <T> void requiredNull(T t, String message, String msgKey) throws TimiException {
if (t != null) {
throw new TimiException(TimiCode.ERROR, message).msgKey(msgKey);
}
}
public static void requiredTrue(boolean bool, String message) throws TimiException { public static void requiredTrue(boolean bool, String message) throws TimiException {
if (bool) { if (bool) {
return; return;
@ -129,11 +142,11 @@ public class TimiException extends RuntimeException implements LanguageMsgMappin
throw new TimiException(TimiCode.ERROR, message); throw new TimiException(TimiCode.ERROR, message);
} }
public static void requiredFalse(boolean bool, String message) throws TimiException { public static void requiredTrue(boolean bool, String message, String msgKey) throws TimiException {
if (!bool) { if (bool) {
return; return;
} }
throw new TimiException(TimiCode.ERROR, message); throw new TimiException(TimiCode.ERROR, message).msgKey(msgKey);
} }
public static void requiredTrue(CallbackReturn<Boolean> callback, String message) throws TimiException { public static void requiredTrue(CallbackReturn<Boolean> callback, String message) throws TimiException {
@ -144,11 +157,11 @@ public class TimiException extends RuntimeException implements LanguageMsgMappin
throw new TimiException(TimiCode.ERROR, message); throw new TimiException(TimiCode.ERROR, message);
} }
public static void requiredFalse(CallbackReturn<Boolean> callback, String message) throws TimiException { public static void requiredTrue(CallbackReturn<Boolean> callback, String message, String msgKey) throws TimiException {
TimiException.required(callback, "not found callback"); TimiException.required(callback, "not found callback");
if (!callback.handler()) { if (callback.handler()) {
return; return;
} }
throw new TimiException(TimiCode.ERROR, message); throw new TimiException(TimiCode.ERROR, message).msgKey(msgKey);
} }
} }

View File

@ -19,9 +19,9 @@ public class TimiResponse<T> implements Serializable, LanguageMsgMapping<TimiRes
/** 消息 */ /** 消息 */
protected String msg; protected String msg;
protected String msgKey; protected transient String msgKey;
protected Map<String, Object> msgArgs; protected transient Map<String, Object> msgArgs;
/** 数据体 */ /** 数据体 */
protected T data; protected T data;