110 lines
3.1 KiB
Java
110 lines
3.1 KiB
Java
package com.imyeyu.spring.util;
|
|
|
|
import com.imyeyu.java.bean.timi.TimiCode;
|
|
import com.imyeyu.java.bean.timi.TimiException;
|
|
import com.imyeyu.java.bean.timi.TimiResponse;
|
|
import jakarta.servlet.ServletException;
|
|
import jakarta.validation.ValidationException;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.TypeMismatchException;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.http.converter.HttpMessageConversionException;
|
|
import org.springframework.validation.BindException;
|
|
import org.springframework.validation.FieldError;
|
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
|
/**
|
|
* 全局异常处理器
|
|
*
|
|
* @author 夜雨
|
|
* @version 2023-05-06 16:28
|
|
*/
|
|
@RestControllerAdvice
|
|
public class GlobalExceptionHandler {
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
|
|
|
/**
|
|
* 创建全局异常处理器
|
|
*/
|
|
public GlobalExceptionHandler() {
|
|
}
|
|
|
|
@Value("${spring.profiles.active}")
|
|
private String env;
|
|
|
|
/**
|
|
* 消息转换异常
|
|
*
|
|
* @param e 异常
|
|
* @return 异常返回
|
|
*/
|
|
@ExceptionHandler(HttpMessageConversionException.class)
|
|
public TimiResponse<?> conversionException(HttpMessageConversionException e) {
|
|
log.warn(e.getMessage());
|
|
if (env.contains("dev") || log.isDebugEnabled()) {
|
|
log.error("conversion error", e);
|
|
}
|
|
return new TimiResponse<>(TimiCode.ARG_BAD).msgKey("invalid.body");
|
|
}
|
|
|
|
/**
|
|
* 请求异常
|
|
*
|
|
* @param e 异常
|
|
* @return 异常返回
|
|
*/
|
|
@ExceptionHandler(ServletException.class)
|
|
public TimiResponse<?> headerException(ServletException e) {
|
|
log.warn(e.getMessage());
|
|
if (env.contains("dev") || log.isDebugEnabled()) {
|
|
log.error("header error", e);
|
|
}
|
|
return new TimiResponse<>(TimiCode.REQUEST_BAD).msgKey("invalid.request");
|
|
}
|
|
|
|
/**
|
|
* 接口入参基本校验异常
|
|
*
|
|
* @param e 异常
|
|
* @return 异常返回
|
|
*/
|
|
@ExceptionHandler({BindException.class, ValidationException.class, MethodArgumentNotValidException.class, TypeMismatchException.class})
|
|
public TimiResponse<?> paramsException(Exception e) {
|
|
if (e instanceof MethodArgumentNotValidException subE) {
|
|
log.warn("request error", e);
|
|
FieldError error = subE.getBindingResult().getFieldError();
|
|
if (error != null) {
|
|
return new TimiResponse<>(TimiCode.ARG_BAD, "[%s] %s".formatted(error.getField(), error.getDefaultMessage()));
|
|
}
|
|
}
|
|
if (env.startsWith("dev") || log.isDebugEnabled()) {
|
|
log.error("request error", e);
|
|
}
|
|
return new TimiResponse<>(TimiCode.REQUEST_BAD).msgKey("invalid.arg");
|
|
}
|
|
|
|
/**
|
|
* 全局异常
|
|
*
|
|
* @param e 异常
|
|
* @return 异常返回
|
|
*/
|
|
@ExceptionHandler(Throwable.class)
|
|
public TimiResponse<?> error(Throwable e) {
|
|
if (e instanceof TimiException timiE) {
|
|
if (!env.startsWith("prod") || log.isDebugEnabled()) {
|
|
log.error(timiE.getMessage(), e);
|
|
}
|
|
// 一般异常
|
|
return timiE.toResponse();
|
|
}
|
|
// 致命异常
|
|
log.error("fatal error", e);
|
|
return new TimiResponse<>(TimiCode.ERROR).msgKey("service.error");
|
|
}
|
|
}
|