Update DefaultExceptionHandlerConfig.java

cyj
pbvfus8to 8 months ago
parent e6482b8aad
commit 1f0d48978e

@ -28,51 +28,86 @@ import java.util.ArrayList;
import java.util.List;
/**
*
*
* `@ExceptionHandler`
*
*
* @author LGH
*/
@Slf4j
// 使用 @Slf4j 注解,由 lombok 自动生成一个名为 log 的日志记录器对象,方便在类中记录日志信息
@Controller
// 使用 @Controller 注解声明该类是一个Spring MVC中的控制器类虽然这里主要功能是异常处理但可能涉及到一些视图相关的处理逻辑如果有的话
@RestControllerAdvice
// 使用 @RestControllerAdvice 注解,使得该类可以作为一个全局的异常处理类,能够捕获并处理整个项目中不同控制器层抛出的异常
public class DefaultExceptionHandlerConfig {
/**
*
* `MethodArgumentNotValidException`使`@Valid``BindException`
*
*
* @param e `MethodArgumentNotValidException``BindException`
* @return `ResponseEntity<ServerResponseEntity<List<String>>>``HttpStatus.OK`
*/
@ExceptionHandler({ MethodArgumentNotValidException.class, BindException.class })
public ResponseEntity<ServerResponseEntity<List<String>>> methodArgumentNotValidExceptionHandler(Exception e) {
log.error("methodArgumentNotValidExceptionHandler", e);
List<FieldError> fieldErrors = null;
// 判断异常类型是否为 MethodArgumentNotValidException如果是则从该异常中获取字段校验错误信息列表
if (e instanceof MethodArgumentNotValidException) {
fieldErrors = ((MethodArgumentNotValidException) e).getBindingResult().getFieldErrors();
}
// 判断异常类型是否为 BindException如果是则从该异常中获取字段校验错误信息列表
if (e instanceof BindException) {
fieldErrors = ((BindException) e).getBindingResult().getFieldErrors();
}
// 如果没有获取到字段校验错误信息列表(可能异常类型不匹配等原因),则直接返回参数校验失败的通用响应
if (fieldErrors == null) {
return ResponseEntity.status(HttpStatus.OK)
.body(ServerResponseEntity.fail(ResponseEnum.METHOD_ARGUMENT_NOT_VALID));
.body(ServerResponseEntity.fail(ResponseEnum.METHOD_ARGUMENT_NOT_VALID));
}
List<String> defaultMessages = new ArrayList<>(fieldErrors.size());
// 遍历字段校验错误信息列表,将每个字段的名称和对应的错误提示信息拼接成字符串后添加到错误信息列表中
for (FieldError fieldError : fieldErrors) {
defaultMessages.add(fieldError.getField() + ":" + fieldError.getDefaultMessage());
}
// 返回包含具体字段校验错误信息列表以及参数校验失败状态码的响应实体对象
return ResponseEntity.status(HttpStatus.OK)
.body(ServerResponseEntity.fail(ResponseEnum.METHOD_ARGUMENT_NOT_VALID, defaultMessages));
.body(ServerResponseEntity.fail(ResponseEnum.METHOD_ARGUMENT_NOT_VALID, defaultMessages));
}
/**
*
* `YamiShopBindException`
*
*
* @param e `YamiShopBindException`
* @return `ResponseEntity<ServerResponseEntity<?>>``HttpStatus.OK`
*/
@ExceptionHandler(YamiShopBindException.class)
public ResponseEntity<ServerResponseEntity<?>> unauthorizedExceptionHandler(YamiShopBindException e){
public ResponseEntity<ServerResponseEntity<?>> unauthorizedExceptionHandler(YamiShopBindException e) {
log.error("mall4jExceptionHandler", e);
ServerResponseEntity<?> serverResponseEntity = e.getServerResponseEntity();
if (serverResponseEntity!=null) {
if (serverResponseEntity!= null) {
return ResponseEntity.status(HttpStatus.OK).body(serverResponseEntity);
}
// 失败返回消息 状态码固定为直接显示消息的状态码
return ResponseEntity.status(HttpStatus.OK).body(ServerResponseEntity.fail(e.getCode(),e.getMessage()));
// 失败返回消息,状态码固定为直接显示消息的状态码,根据异常的代码和消息构建响应实体对象并返回
return ResponseEntity.status(HttpStatus.OK).body(ServerResponseEntity.fail(e.getCode(), e.getMessage()));
}
/**
*
* `NoResourceFoundException`
*
*
* @param e
* @return `ResponseEntity<ServerResponseEntity<Object>>``HttpStatus.OK`
*/
@ExceptionHandler(Exception.class)
public ResponseEntity<ServerResponseEntity<Object>> exceptionHandler(Exception e){
public ResponseEntity<ServerResponseEntity<Object>> exceptionHandler(Exception e) {
if (e instanceof NoResourceFoundException) {
return ResponseEntity.status(HttpStatus.OK).body(ServerResponseEntity.showFailMsg(e.getMessage()));
}

Loading…
Cancel
Save