You've already forked FrameTour-BE
110 lines
4.7 KiB
Java
110 lines
4.7 KiB
Java
package com.ycwl.basic.exception;
|
|
|
|
import com.ycwl.basic.enums.BizCodeEnum;
|
|
import com.ycwl.basic.utils.ApiResponse;
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
import org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.converter.HttpMessageNotReadableException;
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
|
|
|
import java.io.IOException;
|
|
|
|
/**
|
|
* @date 2022年09月23日 10:19
|
|
* 全局异常处理器
|
|
*/
|
|
@RestControllerAdvice(basePackages = {"com.ycwl.basic"})
|
|
public class CustomExceptionHandle {
|
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(CustomExceptionHandle.class);
|
|
|
|
@ExceptionHandler(CheckTokenException.class)
|
|
public ApiResponse tokenExceptionHandler(HttpServletResponse response, BaseException ex) {
|
|
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
|
|
return ApiResponse.buildResponse(200, ex.getMessage());
|
|
}
|
|
|
|
@ExceptionHandler(MissTokenException.class)
|
|
public ApiResponse tokenMissExceptionHandler(HttpServletResponse response, BaseException ex) {
|
|
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
|
|
return ApiResponse.buildResponse(ex.getStatus(), ex.getMessage());
|
|
}
|
|
|
|
@ExceptionHandler(TokenExpireException.class)
|
|
public ApiResponse tokenExpireException(HttpServletResponse response, BaseException ex) {
|
|
response.setStatus(HttpStatus.OK.value());
|
|
return ApiResponse.buildResponse(ex.getStatus(), ex.getMessage());
|
|
}
|
|
|
|
@ExceptionHandler(BaseException.class)
|
|
public ApiResponse baseExceptionHandler(HttpServletResponse response, BaseException ex) {
|
|
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
|
|
return ApiResponse.buildResponse(ex.getStatus(), ex.getMessage());
|
|
}
|
|
|
|
/**
|
|
* 自定义异常统一处理类
|
|
*/
|
|
@ExceptionHandler(value = BizException.class)
|
|
public ApiResponse<String> handle(HttpServletResponse response, BizException bizException) {
|
|
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
|
|
return ApiResponse.buildResponse(bizException.getCode(), bizException.getMsg());
|
|
}
|
|
@ExceptionHandler(value = MethodArgumentTypeMismatchException.class)
|
|
public ApiResponse<String> handle(HttpServletRequest request, HttpServletResponse response, MethodArgumentTypeMismatchException exception) {
|
|
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
|
|
LOGGER.error("参数错误 -> 请求地址:{},请求方式:{}", request.getRequestURI(), request.getMethod());
|
|
return ApiResponse.buildResponse(5000, "参数错误!");
|
|
}
|
|
|
|
@ExceptionHandler(value =IOException.class)
|
|
public ApiResponse<String> handle(HttpServletResponse response, IOException e) {
|
|
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
|
|
LOGGER.error("IO异常 -> {}", e.getMessage(), e);
|
|
return ApiResponse.buildResult(BizCodeEnum.SERVER_UNKONWN_ERROR);
|
|
}
|
|
|
|
/**
|
|
* 异常统一返回处理
|
|
*/
|
|
@ExceptionHandler(value = Exception.class)
|
|
public ApiResponse<String> handle(HttpServletResponse response, Exception e) {
|
|
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
|
|
LOGGER.error("系统异常 -> {}", e.getMessage(), e);
|
|
return ApiResponse.buildResult(BizCodeEnum.SERVER_UNKONWN_ERROR);
|
|
}
|
|
|
|
/**
|
|
* 移动端自定义异常统一处理类
|
|
*/
|
|
@ExceptionHandler(value = AppException.class)
|
|
public ApiResponse<String> handle(HttpServletResponse response, AppException appException) {
|
|
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
|
|
return ApiResponse.buildResponse(appException.getCode(), appException.getMsg());
|
|
}
|
|
|
|
/**
|
|
* 移动端自定义异常统一处理类
|
|
*/
|
|
@ExceptionHandler(value = HttpMessageNotReadableException.class)
|
|
public ApiResponse<String> handle(HttpServletResponse response, HttpMessageNotReadableException httpMessageNotReadableException) {
|
|
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
|
|
return ApiResponse.buildResponse(500, "请求参数格式错误");
|
|
}
|
|
|
|
/**
|
|
* 文件上传超长异常统一处理
|
|
*/
|
|
@ExceptionHandler(value = SizeLimitExceededException.class)
|
|
public ApiResponse<String> handle(SizeLimitExceededException sizeLimitExceededException) {
|
|
return ApiResponse.buildResponse(415, "文件过大,请重新上传");
|
|
}
|
|
}
|