作者 王勇

简单封装自定义异常,未完善

package com.sunyo.wlpt.message.bus.service.exception;
/**
* @author 子诚
* Description:
* 时间:2020/7/17 16:43
*/
public class CustomException extends RuntimeException {
private static final long serialVersionUID = 6098063244016154220L;
/**
* 异常错误编码
*/
private String code;
/**
* 异常信息
*/
private String message;
public CustomException(CustomExceptionType exceptionTypeEnum, String message) {
this.code = exceptionTypeEnum.getCode();
this.message = message;
}
public String getCode() {
return code;
}
@Override
public String getMessage() {
return message;
}
}
... ...
package com.sunyo.wlpt.message.bus.service.exception;
/**
* @author 子诚
* Description:枚举,定制异常类型
* 时间:2020/7/17 16:27
*/
public enum CustomExceptionType {
CLIENT_ERROR("400", "客户端异常"),
SYSTEM_ERROR("500", "系统服务异常"),
OTHER_ERROR("999", "其他未知异常");
/**
* 响应业务状态
*/
private String code;
/**
* 响应消息
*/
private String msg;
CustomExceptionType(String code, String msg) {
this.code = code;
this.msg = msg;
}
public String getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
... ...
package com.sunyo.wlpt.message.bus.service.exception;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
/**
* @author 子诚
* Description:自定义全局异常处理类
* 时间:2020/7/17 17:44
*/
@ControllerAdvice
public class GlobalExceptionHandler {
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
}
... ...
package com.sunyo.wlpt.message.bus.service.response;
import com.sunyo.wlpt.message.bus.service.exception.CustomException;
import com.sunyo.wlpt.message.bus.service.exception.CustomExceptionType;
import lombok.Data;
import java.io.Serializable;
... ... @@ -46,25 +48,76 @@ public class ResultJson<T> implements Serializable {
}
/**
* 有参,构造方法;
* 重载
* 定义有参构造器
*
* @param code 响应状态
* @param msg 响应消息
*/
public ResultJson(String code) {
this.code = code;
}
public ResultJson(T data) {
this.data = data;
}
public ResultJson(String code, String msg) {
this.code = code;
this.msg = msg;
}
/**
* 定义有参构造器
*
* @param code 响应状态
* @param msg 响应消息
* @param data 响应数据
*/
public ResultJson(String code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
/**
* 定义静态、成功方法(重载)
*
* @return 成功(没有响应数据)
*/
public static ResultJson success() {
return new ResultJson<>("200", "success");
}
/**
* 定义静态、成功方法(重载)
*
* @return 成功(响应数据)
*/
public static ResultJson success(Object data) {
return new ResultJson<>("200", "success", data);
}
/**
* 请求出现异常时的响应数据封装
*
* @param e 自定义异常类
* @return 返回异常信息
*/
public static ResultJson error(CustomException e) {
ResultJson result = new ResultJson<>();
result.setCode(e.getCode());
if (e.getCode() == CustomExceptionType.CLIENT_ERROR.getCode()) {
result.setMsg(e.getMessage());
} else if (e.getCode() == CustomExceptionType.SYSTEM_ERROR.getCode()) {
result.setMsg(e.getMessage() + ";请将该异常发送给管理员");
} else {
result.setMsg("系统出现未知异常,请联系管理员!");
}
// 可以尝试着做异常信息持久化
return result;
}
/**
* 请求出现异常时的响应数据封装
*
* @param customExceptionType 自定义异常枚举中的异常
* @param errorMessage 错误信息
* @return 返回异常信息
*/
public static ResultJson error(CustomExceptionType customExceptionType, String errorMessage) {
return new ResultJson<>(customExceptionType.getCode(), errorMessage);
}
}
... ...