//异常信息
@Getter
public enum ResponseCode {
SUCCESS(0, "Success"),
INTERNAL_ERROR(1, "服务器内部错误"),
USER_INPUT_ERROR(2, "用户输入错误"),
AUTHENTICATION_NEEDED(3, "Token过期或无效"),
FORBIDDEN(4, "禁止访问"),
TOO_FREQUENT_VISIT(5, "访问太频繁,请休息一会儿");
private final int code;
private final String message;
private final Response.Status status;
ResponseCode(int code, String message, Response.Status status) {
this.code = code;
this.message = message;
this.status = status;
}
ResponseCode(int code, String message) {
this(code, message, Response.Status.INTERNAL_SERVER_ERROR);
}
}
/**
返回消息类
**/
@Getter
@Setter
public class GenericResponse<T> {
private int code;
private T data;
private String message;
public GenericResponse() {};
public GenericResponse(int code, T data) {
this.code = code;
this.data = data;
}
public GenericResponse(int code, T data, String message) {
this(code, data);
this.message = message;
}
public GenericResponse(ResponseCode responseCode) {
this.code = responseCode.getCode();
this.data = null;
this.message = responseCode.getMessage();
}
public GenericResponse(ResponseCode responseCode, T data) {
this(responseCode);
this.data = data;
}
public GenericResponse(ResponseCode responseCode, T data, String message) {
this(responseCode, data);
this.message = message;
}
}
/**
自定义异常
**/
@Getter
public class AuroraRuntimeException extends RuntimeException {
private final ResponseCode code;
public AuroraRuntimeException() {
super(String.format("%s", ResponseCode.INTERNAL_ERROR.getMessage()));
this.code = ResponseCode.INTERNAL_ERROR;
}
public AuroraRuntimeException(Throwable e) {
super(e);
this.code = ResponseCode.INTERNAL_ERROR;
}
public AuroraRuntimeException(String msg) {
this(ResponseCode.INTERNAL_ERROR, msg);
}
public AuroraRuntimeException(ResponseCode code) {
super(String.format("%s", code.getMessage()));
this.code = code;
}
public AuroraRuntimeException(ResponseCode code, String msg) {
super(msg);
this.code = code;
}
}
/**
全局Rest异常处理类
**/
Log4j2
@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
/**
* 定义要捕获的异常 可以多个 @ExceptionHandler({}) *
* @param request request
* @param e exception
* @param response response
* @return 响应结果
*/
@ExceptionHandler(AuroraRuntimeException.class)
public GenericResponse customExceptionHandler(HttpServletRequest request, final Exception e, HttpServletResponse response) {
AuroraRuntimeException exception = (AuroraRuntimeException) e;
if (exception.getCode() == ResponseCode.USER_INPUT_ERROR) {
response.setStatus(HttpStatus.BAD_REQUEST.value());
} else if (exception.getCode() == ResponseCode.FORBIDDEN) {
response.setStatus(HttpStatus.FORBIDDEN.value());
} else {
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
}
return new GenericResponse(exception.getCode(), null, exception.getMessage());
}
@ExceptionHandler(NotLoginException.class)
public GenericResponse tokenExceptionHandler(HttpServletRequest request, final Exception e, HttpServletResponse response) {
log.error("token exception", e);
response.setStatus(HttpStatus.FORBIDDEN.value());
return new GenericResponse(ResponseCode.AUTHENTICATION_NEEDED);
}
}
/**
用户抛异常
**/
public User getUserInfo(Long userId) {
// some logic
User user = daoFactory.getExtendedUserMapper().selectByPrimaryKey(userId);
if (user == null) {
throw new AuroraRuntimeException(ResponseCode.USER_INPUT_ERROR, "用户id不存在");
}
// some logic
....
}