自定义异常类实现

This commit is contained in:
xiongfeng
2023-11-08 16:04:50 +08:00
parent 0fcf9cc177
commit 67bf832c4a
7 changed files with 226 additions and 4 deletions

View File

@@ -0,0 +1,38 @@
package cn.xf.basedemo.common.exception;
import lombok.Getter;
/**
* @Author: xiongfeng
* @CreateTime: 2023-11-08 13:54
* @Description: TODO 业务统一异常处理类
* @Version: 1.0
*/
@Getter
public class BusinessException extends RuntimeException{
private final ResponseCode code;
public BusinessException() {
super(String.format("%s", ResponseCode.INTERNAL_ERROR.getMessage()));
this.code = ResponseCode.INTERNAL_ERROR;
}
public BusinessException(Throwable e) {
super(e);
this.code = ResponseCode.INTERNAL_ERROR;
}
public BusinessException(String msg) {
this(ResponseCode.INTERNAL_ERROR, msg);
}
public BusinessException(ResponseCode code) {
super(String.format("%s", code.getMessage()));
this.code = code;
}
public BusinessException(ResponseCode code, String msg) {
super(msg);
this.code = code;
}
}

View File

@@ -0,0 +1,47 @@
package cn.xf.basedemo.common.exception;
import lombok.Data;
/**
* @Author: xiongfeng
* @CreateTime: 2023-11-08 13:47
* @Description: TODO
* @Version: 1.0
*/
@Data
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;
}
}

View File

@@ -0,0 +1,61 @@
package cn.xf.basedemo.common.exception;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @Author: xiongfeng
* @CreateTime: 2023-11-08 11:48
* @Description: TODO 全局异常捕获类
* @Version: 1.0
*/
@Slf4j
@RestControllerAdvice
@Component
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler{
/**
* 定义要捕获的异常 可以多个 @ExceptionHandler({}) *
* @param request request
* @param e exception
* @param response response
* @return 响应结果
*/
@ExceptionHandler(BusinessException.class)
public GenericResponse customExceptionHandler(HttpServletRequest request, final Exception e, HttpServletResponse response) {
BusinessException exception = (BusinessException) 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());
}
/**
* 登录异常捕获
* @param request
* @param e
* @param response
* @return
*/
@ExceptionHandler(LoginException.class)
public GenericResponse tokenExceptionHandler(HttpServletRequest request, final Exception e, HttpServletResponse response) {
log.error("token exception", e);
LoginException exception = (LoginException) e;
response.setStatus(HttpStatus.FORBIDDEN.value());
return new GenericResponse(exception.getCode(),null,exception.getMessage());
}
}

View File

@@ -0,0 +1,40 @@
package cn.xf.basedemo.common.exception;
import lombok.Getter;
/**
* @Author: xiongfeng
* @CreateTime: 2023-11-08 13:46
* @Description: TODO 登录异常类
* @Version: 1.0
*/
@Getter
public class LoginException extends RuntimeException{
private final ResponseCode code;
public LoginException() {
super(String.format("%s", ResponseCode.AUTHENTICATION_NEEDED.getMessage()));
this.code = ResponseCode.AUTHENTICATION_NEEDED;
}
public LoginException(Throwable e) {
super(e);
this.code = ResponseCode.AUTHENTICATION_NEEDED;
}
public LoginException(String msg) {
this(ResponseCode.AUTHENTICATION_NEEDED, msg);
}
public LoginException(ResponseCode code) {
super(String.format("%s", code.getMessage()));
this.code = code;
}
public LoginException(ResponseCode code, String msg) {
super(msg);
this.code = code;
}
}

View File

@@ -0,0 +1,35 @@
package cn.xf.basedemo.common.exception;
import lombok.Getter;
/**
* @Author: xiongfeng
* @CreateTime: 2023-11-08 13:41
* @Description: TODO 异常状态码
* @Version: 1.0
*/
@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;
ResponseCode(Integer code, String message) {
this.code = code;
this.message = message;
}
}

View File

@@ -1,6 +1,5 @@
package cn.xf.basedemo.interceptor;
import cn.xf.basedemo.interceptor.TokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

View File

@@ -1,5 +1,7 @@
package cn.xf.basedemo.interceptor;
import cn.xf.basedemo.common.exception.LoginException;
import cn.xf.basedemo.common.exception.ResponseCode;
import cn.xf.basedemo.common.model.LoginUser;
import cn.xf.basedemo.common.utils.ApplicationContextUtils;
import com.alibaba.fastjson.JSONObject;
@@ -42,17 +44,17 @@ public class TokenInterceptor implements HandlerInterceptor {
if(StringUtils.isEmpty(token))
token = request.getParameter("token");
if(StringUtils.isEmpty(token)){
return false;
throw new LoginException("请先登录");
}
String value = (String) redisTemplate.opsForValue().get("token:" + token);
if(StringUtils.isEmpty(value)){
return false;
throw new LoginException();
}
JSONObject jsonObject = JSONObject.parseObject(value);
//JSON对象转换成Java对象
LoginUser loginUserInfo = JSONObject.toJavaObject(jsonObject, LoginUser.class);
if(loginUserInfo == null || loginUserInfo.getId() <= 0){
return false;
throw new LoginException(ResponseCode.USER_INPUT_ERROR);
}
redisTemplate.expire(token, 86700, TimeUnit.SECONDS);