报错返回语言适配
This commit is contained in:
@@ -1,9 +1,17 @@
|
||||
package com.aida.seller.common.exception;
|
||||
|
||||
import com.aida.seller.common.context.UserContext;
|
||||
import com.aida.seller.model.vo.AuthPrincipalVo;
|
||||
import com.aida.seller.common.result.ResultEnum;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.PropertyResourceBundle;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
/**
|
||||
* @author: dwjian
|
||||
* @description: 业务异常
|
||||
@@ -12,31 +20,64 @@ import lombok.extern.slf4j.Slf4j;
|
||||
@Slf4j
|
||||
public class BusinessException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private Integer code;
|
||||
private String msg;
|
||||
|
||||
public BusinessException(ResultEnum resultEnum) {
|
||||
this.code = resultEnum.getCode();
|
||||
this.msg = resultEnum.getMsg();
|
||||
this.msg = getMessageFromResource(resultEnum.getMsg(), getUserLocale());
|
||||
}
|
||||
|
||||
public BusinessException(String msg) {
|
||||
this.code = ResultEnum.FAIL.getCode();
|
||||
this.msg = msg;
|
||||
this.msg = getMessageFromResource(msg, getUserLocale());
|
||||
}
|
||||
|
||||
public BusinessException(String msg, Integer code) {
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
this.msg = getMessageFromResource(msg, getUserLocale());
|
||||
}
|
||||
|
||||
public BusinessException(Throwable cause) {
|
||||
this.code = ResultEnum.FAIL.getCode();
|
||||
this.msg = cause.getMessage();
|
||||
this.msg = getMessageFromResource(cause.getMessage(), getUserLocale());
|
||||
}
|
||||
|
||||
public BusinessException(ResultEnum resultEnum, String customMsg) {
|
||||
this.code = resultEnum.getCode();
|
||||
this.msg = customMsg;
|
||||
this.msg = getMessageFromResource(customMsg, getUserLocale());
|
||||
}
|
||||
|
||||
private static String getUserLocale() {
|
||||
try {
|
||||
AuthPrincipalVo userInfo = UserContext.getUserHolder();
|
||||
if (userInfo == null) return "en";
|
||||
String lang = userInfo.getLanguage();
|
||||
if (lang == null) return "en";
|
||||
if ("CHINESE_SIMPLIFIED".equalsIgnoreCase(lang)) return "zh";
|
||||
if ("ENGLISH".equalsIgnoreCase(lang)) return "en";
|
||||
return "en";
|
||||
} catch (Exception e) {
|
||||
return "en";
|
||||
}
|
||||
}
|
||||
|
||||
public static String getMessageFromResource(String msg, String locale) {
|
||||
if (msg == null) return null;
|
||||
try (InputStream is = BusinessException.class.getClassLoader()
|
||||
.getResourceAsStream("messages_" + locale + ".properties")) {
|
||||
if (is != null) {
|
||||
ResourceBundle bundle = new PropertyResourceBundle(
|
||||
new InputStreamReader(is, StandardCharsets.UTF_8));
|
||||
if (bundle.containsKey(msg)) {
|
||||
return bundle.getString(msg);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to load messages_{}.properties: {}", locale, e.getMessage());
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user