137 lines
3.7 KiB
Java
137 lines
3.7 KiB
Java
package com.isu.gaswellwatch.exception;
|
||
|
||
import lombok.AllArgsConstructor;
|
||
import lombok.Data;
|
||
import lombok.EqualsAndHashCode;
|
||
import org.slf4j.Logger;
|
||
import org.slf4j.LoggerFactory;
|
||
|
||
import java.util.ArrayList;
|
||
import java.util.List;
|
||
|
||
@Data
|
||
@AllArgsConstructor
|
||
@EqualsAndHashCode(callSuper = true)
|
||
public class BusinessException extends RuntimeException {
|
||
/**
|
||
* <li>日志</li>
|
||
*/
|
||
private static Logger logger = LoggerFactory.getLogger(BusinessException.class);
|
||
|
||
/**
|
||
* <li>状态码</li>
|
||
*/
|
||
private Integer code = 500;
|
||
|
||
/**
|
||
* <li>字符串错误信息(和i18n无关)</li>
|
||
*/
|
||
private String message;
|
||
/**
|
||
* 国际化文件中需要被替换的字符集合
|
||
*/
|
||
private List<String> replaceCharacters = null;
|
||
|
||
public BusinessException(int code, String message) {
|
||
this.code = code;
|
||
this.message = message;
|
||
}
|
||
|
||
/**
|
||
* 默认构造方法
|
||
*/
|
||
public BusinessException() {
|
||
super();
|
||
}
|
||
|
||
/**
|
||
* 重载构造函数
|
||
*
|
||
* @param message
|
||
*/
|
||
public BusinessException(String message) {
|
||
this.message = message;
|
||
}
|
||
|
||
/**
|
||
* 重载构造函数
|
||
*
|
||
* @param message 消息编码
|
||
* @param replaceCharacters 国际化文件中需要被替换的字符串集合
|
||
*/
|
||
public BusinessException(String message, List<String> replaceCharacters) {
|
||
this.message = message;
|
||
this.replaceCharacters = replaceCharacters;
|
||
}
|
||
|
||
/**
|
||
* 重载构造函数
|
||
*
|
||
* @param message 消息编码
|
||
* @param replaceCharacters 国际化文件中需要被替换的字符串集合
|
||
*/
|
||
public BusinessException(String message, String... replaceCharacters) {
|
||
|
||
this.message = message;
|
||
if (replaceCharacters != null && replaceCharacters.length > 0) {
|
||
this.replaceCharacters = new ArrayList<>();
|
||
for (String replaceCharacter : replaceCharacters) {
|
||
this.replaceCharacters.add(replaceCharacter);
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 构造方法
|
||
*
|
||
* @param message 消息编码
|
||
* @param cause 异常原因
|
||
*/
|
||
public BusinessException(String message, Throwable cause) {
|
||
super(message, cause);
|
||
this.message = message;
|
||
}
|
||
|
||
/**
|
||
* 重载构造函数
|
||
*
|
||
* @param message 消息编码
|
||
* @param replaceCharacters 国际化文件中需要被替换的字符串集合
|
||
* @param cause 错误原因
|
||
*/
|
||
public BusinessException(String message, List<String> replaceCharacters, Throwable cause) {
|
||
super(message, cause);
|
||
this.message = message;
|
||
this.replaceCharacters = replaceCharacters;
|
||
}
|
||
|
||
/**
|
||
* 获得出错信息. 读取i18N properties文件中 messageCode对应的message,并组合参数获得i18n的出错信息.
|
||
*
|
||
* @return 返回 message
|
||
*/
|
||
@Override
|
||
public String getMessage() {
|
||
logger.debug("businessException code:" + message);
|
||
|
||
// 否则用messageCode查询Properties文件获得出错信息
|
||
String result = null;
|
||
|
||
// message = "Message Code is: " + messageCode + ", but can't get the message of the Message Code";
|
||
result = this.message;
|
||
|
||
// 是否有需要替换的字符
|
||
if ((replaceCharacters != null) && (replaceCharacters.size() > 0)) {
|
||
for (int i = 0; i < replaceCharacters.size(); i++) {
|
||
String tempChar = "{" + i + "}";
|
||
|
||
if (result.indexOf(tempChar) != -1) {
|
||
result = result.replace(tempChar, replaceCharacters.get(i));
|
||
}
|
||
}
|
||
}
|
||
|
||
return result;
|
||
}
|
||
}
|