Procházet zdrojové kódy

修改异常捕获

tags/24080901
WendyYang před 3 měsíci
rodič
revize
b0f81d77f3
1 změnil soubory, kde provedl 21 přidání a 12 odebrání
  1. +21
    -12
      hz-pm-api/src/main/java/com/hz/pm/api/common/handler/GlobalExceptionHandler.java

+ 21
- 12
hz-pm-api/src/main/java/com/hz/pm/api/common/handler/GlobalExceptionHandler.java Zobrazit soubor

@@ -2,9 +2,11 @@ package com.hz.pm.api.common.handler;

import com.ningdatech.basic.enumeration.Status;
import com.ningdatech.basic.model.ApiResponse;
import com.ningdatech.basic.util.CollUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
@@ -18,9 +20,12 @@ import javax.validation.ConstraintViolationException;
import java.util.stream.Collectors;

/**
* @description: 统一错误处理
* @author: liuxinxin
* @date: 2023/01/03 11:39
* <p>
* GlobalExceptionHandler
* </p>
*
* @author WendyYang
* @since 17:57 2024/6/6
*/
@Slf4j
@ControllerAdvice
@@ -30,7 +35,6 @@ public class GlobalExceptionHandler {
@ResponseBody
@ExceptionHandler(value = NoHandlerFoundException.class)
public ApiResponse<Void> noHandlerFoundException(NoHandlerFoundException e) {
log.error("【全局异常拦截】NoHandlerFoundException: 请求方法 {}, 请求路径 {}", e.getRequestURL(), e.getHttpMethod());
return ApiResponse.ofStatus(Status.REQUEST_NOT_FOUND);
}

@@ -39,24 +43,29 @@ public class GlobalExceptionHandler {
public ApiResponse<Void> bindException(BindException e) {
String msg = e.getAllErrors().stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage)
.collect(Collectors.joining(","));
return ApiResponse.of(Status.BAD_REQUEST.getCode(), msg, null);
.collect(Collectors.joining(" "));
return ApiResponse.of(Status.BAD_REQUEST.getCode(), msg);
}

@ResponseBody
@ExceptionHandler(value = ConstraintViolationException.class)
public ApiResponse<Void> constraintViolationException(ConstraintViolationException e) {
String msg = e.getConstraintViolations().stream()
.map(ConstraintViolation::getMessage)
.collect(Collectors.joining(","));
return ApiResponse.of(Status.BAD_REQUEST.getCode(), msg, null);
String msg = CollUtils.join(e.getConstraintViolations(), ConstraintViolation::getMessage, " ");
return ApiResponse.of(Status.BAD_REQUEST.getCode(), msg);
}

@ResponseBody
@ExceptionHandler(value = AccessDeniedException.class)
public ApiResponse<Void> accessDeniedException(AccessDeniedException e) {
log.error("异常信息: {} ", e.getMessage(), e);
return ApiResponse.of(Status.BAD_REQUEST.getCode(), "暂无操作权限");
}

@ResponseBody
@ExceptionHandler(value = Exception.class)
public ApiResponse<Void> handlerException(Exception e) {
log.error("【全局异常拦截】: 异常信息 {}: {} ", e.getClass().getSimpleName(), e);
return ApiResponse.of(Status.BAD_REQUEST.getCode(), e.getMessage(), null);
log.error("异常信息: {} ", e.getMessage(), e);
return ApiResponse.of(Status.BAD_REQUEST.getCode(), "系统异常,请联系管理员");
}

}

Načítá se…
Zrušit
Uložit