/**
-
@Description: 统一的异常处理
*/
@ControllerAdvice
public class ExceptionHandle {
private static Logger logger = LogConstant.getSysLog();@ExceptionHandler(value = Exception.class)
@ResponseBody
public HttpResponse<ActionResponse> handleException(Exception e){
if(e instanceof SmartTerminalEexception) {
SmartTerminalEexception ex = (SmartTerminalEexception)e;
logger.error("统一异常自定义:msg=" + ex.getMessage() + ",log:" + ex.getLog(), e);
return HttpResponse.build(ex.getCode(), ex.getMessage(), null);
}else if (e instanceof MethodArgumentNotValidException){
MethodArgumentNotValidException ex = (MethodArgumentNotValidException) e;
List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
FieldError error = fieldErrors.get(0);
String msg = error.getDefaultMessage();
logger.error("统一异常自定义:msg=" + msg + ",log:" + msg, ex);
return HttpResponse.build(ResultCode.EXCEPTION, msg, null);
} else if(e instanceof UndeclaredThrowableException) {
SmartTerminalEexception ex = (SmartTerminalEexception) e.getCause();
logger.error("统一异常自定义:msg=" + ex.getMessage() + ",log:" + ex.getMessage(), ex);
return HttpResponse.build(ex.getCode(), ex.getMessage(), null);
}else if (e instanceof UnauthorizedException){
logger.error("统一异常自定义:msg=" + e.getMessage() + ",log:" + e.getMessage(), e);
return HttpResponse.build(ManagerExceptionMsg.ERROR_PRESSION_NOTPRESSION ,"权限不足,请联系管理员",null);
} else{
logger.error("统一异常系统:msg=" + e.getMessage() , e);
return HttpResponse.build(ResultCode.EXCEPTION, "网络异常,请稍后重试", null);
}
}
}
public class SmartTerminalEexception extends Exception implements Serializable {
public SmartTerminalEexception(String code, String message, String log) {
super(message);
this.code = code;
this.log = log;
}
/**
*
*/
private static final long serialVersionUID = 7250629556180017484L;
/**
* 错误响应码
*/
private String code;
/**
* 日志信息(统一异常处理答应)
*/
private String log;
public String getCode() {
return code;
}
public String getLog() {
return log;
}
public void setCode(String code) {
this.code = code;
}
public void setLog(String log) {
this.log = log;
}
}
**
-
@Description: 系统的错误响应码和信息
/
public abstract class SmartTerminalExceptionMsg {
/*- 异常Map,key是异常码,value是异常信息
*/
public static Map<String, String> msg = new HashMap<String, String>();
/**
- @param code 错误码
- @param log 错误日志
- @param args 错误参数
- @return
- @throws Exception
- @Description: 异常构建
/
protected SmartTerminalEexception build(String code, String log, String... args) throws Exception {
String message = msg.get(code);
if (args != null && args.length != 0) {
String result = new String(message);
for (int i = 0; i < args.length; i++) {
result = result.replace("{" + i + "}", args[i]);
}
message = result;
}
return new SmartTerminalEexception(code, message, log);
}
/* - Error Code,
- 共6位,1:系统异常码和值定义在该类中,大家公用
- 前2位表示大板块(10:系统,11:管理后台web模块,12:外部接口,13:订单接口,14:模块4 , 。。。。。)
- 第3,4位表示项目的小模块,5,6位代表具体错误
/
/* ========== ERROR CODE =========== /
/ - 渠道编号不能为空
**/
public static final String ERR_CHANNEL_ISNULL = "100001";
public static final String ERROR_PARAMS_EXCEPTION = "110002";
/**
- 渠道编号不存在
/
public static final String ERR_CHANNEL_ISNOTEXIST = "100002";
/ - 签名异常
**/
public static final String ERR_SIGN_EXCEPTION = "100003";
/**
- 签名异常
**/
public static final String ERR_SORT_EXCEPTION = "100004";
- 异常Map,key是异常码,value是异常信息
/** ========== ERROR MSG =========== **/
static {
msg.put(ERR_CHANNEL_ISNULL, "渠道编号不能为空");
msg.put(ERR_CHANNEL_ISNOTEXIST, "渠道编号不存在");
msg.put(ERR_SIGN_EXCEPTION, "签名异常");
msg.put(ERR_SORT_EXCEPTION, "排序异常");
}
}
/**
-
@Description: Http的响应大对象
*/
public class HttpResponse<T extends ActionResponse> extends Model {
@ApiModelProperty(notes = "响应码",required = true)
private String code;@ApiModelProperty(notes = "响应时间",required = true)
private Date time;@ApiModelProperty(notes = "响应的信息(一般为错误信息)",required = true)
private String msg;@ApiModelProperty(notes = "响应数据",required = true)
private T data;@ApiModelProperty(notes = "32位随机码",required = false, allowEmptyValue = true)
private String randomCode;@ApiModelProperty(notes = "响应数据加密字符串",required = false)
private String srs;
private HttpResponse() {
}
private HttpResponse(String code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
this.time = new Date();
}
/**
* @param code
* @param msg
* @param data
* @return
* @Description: 构建一个响应
*/
public static <T extends ActionResponse> HttpResponse<T> build(String code, String msg, T data) {
return new HttpResponse<T>(code, msg, data);
}
/**
* @param msg
* @param data
* @return
* @Description: 构建一个成功带数据和msg的响应
*/
public static <T extends ActionResponse> HttpResponse<T> build(String msg, T data) {
return build(ResultCode.SUCCESS, msg, data);
}
/**
* @param data
* @return
* @Description: 构建一个成功带响应数据的响应(不带msg)
*/
public static <T extends ActionResponse> HttpResponse<T> build(T data) {
return build(null, data);
}
/**
* @return
* @Description: 构建一个空的默认的成功响应
*/
public static <T extends ActionResponse> HttpResponse<T> build() {
return build(null);
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public String getSrs() {
return srs;
}
public void setSrs(String srs) {
this.srs = srs;
}
public String getRandomCode() {
return randomCode;
}
public void setRandomCode(String randomCode) {
this.randomCode = randomCode;
}
}
/**
-
@Description: 异常工厂,各个模块交给Spring管理,无须每次新增实例
/
public class ExceptionFactory {
/*- 具体的异常类
/
private SmartTerminalExceptionMsg msg = null;
/* - 工厂
*/
private static ExceptionFactory factory = null;
/**
- 该实例为单例
*/
private ExceptionFactory(SmartTerminalExceptionMsg msg) {
this.msg = msg;
}
/**
- @param msg
- @return
- @Description: 获取对象
*/
public static ExceptionFactory getInstance(SmartTerminalExceptionMsg msg) {
synchronized (ExceptionFactory.class) {
if (factory == null) {
factory = new ExceptionFactory(msg);
}
return factory;
}
}
/**
- @param code
- @param log
- @param args
- @return
- @throws Exception
- @Description: 工厂生成异常对象
*/
public SmartTerminalEexception create(String code, String log, String... args) throws Exception {
return msg.build(code, log, args);
}
}
- 具体的异常类
/**
- @Description: 系统响应的常量
/
public interface ResultCode {
/*- 成功响应的响应码
/
public static final String SUCCESS = "200";
/* - 系统异常的异常响应码
*/
public static final String EXCEPTION = "1000";
}
- 成功响应的响应码