gasWellWatch/src/main/java/com/isu/gaswellwatch/exception/BusinessException.java

137 lines
3.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}