LiiuZeYu_branch
lzy 9 months ago
parent ffcb1a13fb
commit f4b1f98cc4

@ -0,0 +1,133 @@
/*
* Copyright (c) 2018-2999 广 All rights reserved.
*
* https://www.mall4j.com/
*
*
*
*
*/
package com.yami.shop.common.config;
// 导入相关的自定义异常类、响应相关的枚举、响应实体类等
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ResponseEnum;
import com.yami.shop.common.response.ServerResponseEntity;
// 导入日志相关的Lombok注解用于简化日志记录代码
import lombok.extern.slf4j.Slf4j;
// 导入Spring的HTTP状态码相关枚举类
import org.springframework.http.HttpStatus;
// 导入Spring用于构建HTTP响应实体的类
import org.springframework.http.ResponseEntity;
// 导入Spring用于标记控制器类的注解此处结合异常处理相关特性使用
import org.springframework.stereotype.Controller;
// 导入Spring在数据校验场景下的异常类以及表示字段错误的类
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
// 导入Spring用于定义异常处理方法的注解以及标记全局异常处理类的注解
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
// 导入Spring用于处理资源未找到异常的类
import org.springframework.web.servlet.resource.NoResourceFoundException;
import java.util.ArrayList;
import java.util.List;
/**
*
*
* @author LGH
*/
@Slf4j
@Controller
@RestControllerAdvice
public class DefaultExceptionHandlerConfig {
/**
* MethodArgumentNotValidExceptionBindException
* Spring
*
*
* @param e MethodArgumentNotValidExceptionBindException
* @return ResponseEntity<ServerResponseEntity<List<String>>> HTTP
*
*/
@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));
}
// 用于存放每个字段的错误信息字符串(格式为:字段名:默认错误消息)
List<String> defaultMessages = new ArrayList<>(fieldErrors.size());
// 遍历字段错误列表拼接每个字段的错误信息字符串并添加到defaultMessages列表中
for (FieldError fieldError : fieldErrors) {
defaultMessages.add(fieldError.getField() + ":" + fieldError.getDefaultMessage());
}
// 返回一个包含具体字段错误信息列表的失败响应实体,使用参数校验不通过的响应枚举标识
return ResponseEntity.status(HttpStatus.OK)
.body(ServerResponseEntity.fail(ResponseEnum.METHOD_ARGUMENT_NOT_VALID, defaultMessages));
}
/**
* YamiShopBindException
* YamiShopBindException
*
*
* @param e YamiShopBindException
* @return ResponseEntity<ServerResponseEntity<?>> HTTP
*
*/
@ExceptionHandler(YamiShopBindException.class)
public ResponseEntity<ServerResponseEntity<?>> unauthorizedExceptionHandler(YamiShopBindException e){
// 记录异常信息,方便后续排查问题,了解异常出现的具体情况
log.error("mall4jExceptionHandler", e);
ServerResponseEntity<?> serverResponseEntity = e.getServerResponseEntity();
// 如果异常中已经封装好了响应实体(在抛出异常时可能已经构建好了特定的响应内容)
if (serverResponseEntity!=null) {
// 直接将其作为响应内容返回给客户端设置状态码为OK
return ResponseEntity.status(HttpStatus.OK).body(serverResponseEntity);
}
// 失败返回消息,状态码固定为直接显示消息的状态码,根据异常的错误码和错误消息构建失败响应实体并返回
return ResponseEntity.status(HttpStatus.OK).body(ServerResponseEntity.fail(e.getCode(),e.getMessage()));
}
/**
* Exception
*
*
*
* @param e Exception
* @return ResponseEntity<ServerResponseEntity<Object>> HTTP
*
*/
@ExceptionHandler(Exception.class)
public ResponseEntity<ServerResponseEntity<Object>> exceptionHandler(Exception e){
// 判断异常是否是资源未找到异常类型
if (e instanceof NoResourceFoundException) {
// 如果是则返回一个包含异常消息的失败响应实体告知客户端资源未找到相关错误信息状态码设为OK
return ResponseEntity.status(HttpStatus.OK).body(ServerResponseEntity.showFailMsg(e.getMessage()));
}
// 记录异常信息,方便后续排查问题,知道出现异常的具体情况
log.error("exceptionHandler", e);
// 返回一个使用默认的异常响应枚举构建的失败响应实体,告知客户端出现了未预期的通用异常情况
return ResponseEntity.status(HttpStatus.OK).body(ServerResponseEntity.fail(ResponseEnum.EXCEPTION));
}
}
Loading…
Cancel
Save