HIS医保

This commit is contained in:
2025-05-29 17:41:06 +08:00
parent d8d53ebb22
commit 9c08c6e7d4
7 changed files with 393 additions and 116 deletions

View File

@@ -18,10 +18,12 @@ import org.springframework.web.method.annotation.MethodArgumentTypeMismatchExcep
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* controller控制器异常处理接口实现类
* <p>其它module可以集成此类进行controller层的异常处理</p>
*
* @author <a href="https://gitee.com/shi-chongli">石头人</a>
*/
@Slf4j
@@ -38,23 +40,18 @@ public class ControllerAdvice {
*/
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public Result<String> bindingException(MethodArgumentNotValidException e) {
// 获得所有校验出错的返回集
BindingResult bindingResult = e.getBindingResult();
List<FieldError> fieldErrors = bindingResult.getFieldErrors();
// 循环获得所有校验异常的字段
Map<String, String> fieldMap = new HashMap<>();
for (FieldError fieldError : fieldErrors) {
fieldMap.put(fieldError.getField(), fieldError.getDefaultMessage());
}
String errMsg = fieldMap.values().toString().replaceAll("]", "").replaceAll("\\[", "");
log.warn(errMsg);
// 返回给前端
String errMsg = e.getBindingResult()
.getFieldErrors()
.parallelStream()
.map(m -> /*m.getField() + ":" +*/ m.getDefaultMessage())
.collect(Collectors.joining(""));
log.error(e.getMessage(), e);
return Result.error(errMsg);
}
/**
* 处理空指针异常
*
* @param nullPointerException 空指针异常
* @return Result
*/
@@ -66,13 +63,14 @@ public class ControllerAdvice {
/**
* 所有的运行时异常,抛出异常
*
* @param throwable 异常
* @return Result
*/
@ExceptionHandler(value = Throwable.class)
public Result<String> handleException(Throwable throwable) {
log.error("异常类型: {}, {}, 信息为: {}", throwable.getCause(), throwable.getClass(), throwable.getMessage());
if (throwable instanceof RRException){
if (throwable instanceof RRException) {
RRException rrException = (RRException) throwable;
return Result.error(rrException.getCode(), rrException.getMsg());
}
@@ -81,24 +79,26 @@ public class ControllerAdvice {
/**
* http信息无可读
*
* @param e 异常
* @return Result
*/
@ExceptionHandler(value = HttpMessageNotReadableException.class)
public Result<String> httpMessageNotReadAbleException(HttpMessageNotReadableException e){
public Result<String> httpMessageNotReadAbleException(HttpMessageNotReadableException e) {
log.warn("异常类型: {} 无可读信息: ", e.getClass(), e);
return Result.error(ErrorEnum.HTTP_MESSAGE_NOT_READABLE_EXCEPTION);
}
/**
* 运行时异常
*
* @param e 运行异常对象
* @return Result
*/
@ExceptionHandler(value = RuntimeException.class)
public Result<String> runtimeException(RuntimeException e){
public Result<String> runtimeException(RuntimeException e) {
log.error("运行时异常:", e);
if (e instanceof RRException){
if (e instanceof RRException) {
RRException rrException = (RRException) e;
return Result.error(rrException.getCode(), rrException.getMsg());
}
@@ -107,31 +107,34 @@ public class ControllerAdvice {
/**
* 请求不支持
*
* @return Result
*/
@ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
public Result<String> httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e){
public Result<String> httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
log.warn("暂不支持该请求: ", e);
return Result.error("暂不支持此请求方式");
}
/**
* 参数类型错误
*
* @return Result
*/
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public Result<String> methodArgument(MethodArgumentTypeMismatchException e){
public Result<String> methodArgument(MethodArgumentTypeMismatchException e) {
log.warn("参数发生错误: ", e);
return Result.error("参数发生错误");
}
/**
* 缺少请求参数
*
* @param e 缺少请求参数异常
* @return Result
*/
@ExceptionHandler(MissingServletRequestParameterException.class)
public Result<String> exception(MissingServletRequestParameterException e){
public Result<String> exception(MissingServletRequestParameterException e) {
log.warn("缺少请求参数: ", e);
return Result.error("缺少请求参数");
}