package jnpf.workflow.admin.handle; import java.util.List; import java.util.stream.Collectors; import org.springframework.context.support.DefaultMessageSourceResolvable; import org.springframework.validation.BindException; import org.springframework.validation.FieldError; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import jakarta.servlet.http.HttpServletRequest; import jnpf.workflow.admin.result.Result; import jnpf.workflow.common.exception.BizException; import jnpf.workflow.common.exception.ResultCode; import lombok.extern.slf4j.Slf4j; @Slf4j @RestControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(BindException.class) public Result bindExceptionHandler(BindException e) { log.error("接口校验失败!原因是:{}", e.getMessage(), e); List fieldErrors = e.getBindingResult().getFieldErrors(); List collect = fieldErrors.stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.toList()); String msg = String.join(",", collect); return Result.failed(ResultCode.REQUEST_PARAM_IS_NULL, msg); } @ExceptionHandler(NullPointerException.class) public Result exceptionHandler(HttpServletRequest req, NullPointerException e) { log.error("发生空指针异常!原因是:{}", e.getMessage(), e); return Result.failed(ResultCode.SYSTEM_EXECUTION_ERROR); } @ExceptionHandler(BizException.class) public Result exceptionHandler(HttpServletRequest req, BizException e) { log.error("BizException!原因是:{}", e.getMessage(), e); return Result.failed(e.getResultCode(), e.getMessage()); } @ExceptionHandler(Exception.class) public Result exceptionHandler(HttpServletRequest req, Exception e) { log.error("未知异常!原因是:{}", e.getMessage(), e); return Result.failed(e.getMessage()); } }