Kaynağa Gözat

捕获异常代码合并

small 2 yıl önce
ebeveyn
işleme
0c487e20b4

+ 0 - 113
sckw-common/sckw-common-core/src/main/java/com/sckw/core/global/GlobalExceptionHandler.java

@@ -1,113 +0,0 @@
-package com.sckw.core.global;
-
-import com.sckw.core.exception.BusinessException;
-import com.sckw.core.exception.NotLoginException;
-import com.sckw.core.web.constant.HttpStatus;
-import com.sckw.core.web.response.HttpResult;
-import jakarta.validation.ConstraintViolation;
-import jakarta.validation.ConstraintViolationException;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.util.CollectionUtils;
-import org.springframework.validation.FieldError;
-import org.springframework.web.bind.MethodArgumentNotValidException;
-import org.springframework.web.bind.annotation.ControllerAdvice;
-import org.springframework.web.bind.annotation.ExceptionHandler;
-import org.springframework.web.bind.annotation.ResponseBody;
-
-import java.util.List;
-import java.util.Set;
-
-/**
- * @Author yzc
- * @Description 统一异常处理
- * @createTime 2023年06月08日 10:59:00
- */
-@Slf4j
-@ControllerAdvice
-public class GlobalExceptionHandler {
-
-    /**
-     * exception处理
-     *
-     * @param ex
-     * @returnh
-     */
-    @ResponseBody
-    @ExceptionHandler(Exception.class)
-    public HttpResult defaultExceptionHandler(Exception ex) {
-        log.error("系统异常", ex);
-        return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, HttpStatus.GLOBAL_EXCEPTION_MESSAGE);
-    }
-
-    /**
-     * BusinessException处理
-     *
-     * @param ex
-     * @return
-     */
-    @ResponseBody
-    @ExceptionHandler(BusinessException.class)
-    public HttpResult businessExceptionHandler(BusinessException ex) {
-        log.info("业务异常,message={},param={}", ex.getMsg(), ex.getParam());
-        return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, ex.getMessage());
-    }
-
-    /**
-     * NotLoginException处理
-     *
-     * @param ex
-     * @return
-     */
-    @ResponseBody
-    @ExceptionHandler(NotLoginException.class)
-    public HttpResult notLoginException(NotLoginException ex) {
-        log.info("用户未登录, message={}, param={}", ex.getMsg(), ex.getParam());
-        return HttpResult.error(HttpStatus.UN_LOGIN_CODE, HttpStatus.UN_LOGIN_MESSAGE, ex.getMessage());
-    }
-
-
-    /**
-     * 注解校验异常处理
-     *
-     * @param ex
-     * @return
-     */
-    @ResponseBody
-    @ExceptionHandler(MethodArgumentNotValidException.class)
-    public HttpResult methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException ex) {
-        List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
-        StringBuilder sb = new StringBuilder();
-        if (!CollectionUtils.isEmpty(fieldErrors)) {
-            boolean first = true;
-            for (FieldError fieldError : fieldErrors) {
-                if (!first) {
-                    sb.append(",");
-                }
-                sb.append(fieldError.getDefaultMessage());
-                first = false;
-            }
-        }
-        String errMsg = sb.toString();
-        log.info("参数校验异常m:{}", errMsg);
-        return HttpResult.error(HttpStatus.PARAMETERS_PATTERN_ERROR_CODE, errMsg);
-    }
-
-    @ResponseBody
-    @ExceptionHandler(ConstraintViolationException.class)
-    public HttpResult constraintViolationExceptionHandler(ConstraintViolationException ex) {
-        log.info("参数校验异常c:{}", ex.getMessage());
-        Set<ConstraintViolation<?>> constraintViolations = ex.getConstraintViolations();
-        StringBuilder sb = new StringBuilder();
-        if (!CollectionUtils.isEmpty(constraintViolations)) {
-            boolean first = true;
-            for (ConstraintViolation<?> constraintViolation : constraintViolations) {
-                if (!first) {
-                    sb.append(",");
-                }
-                sb.append(constraintViolation.getMessage());
-                first = false;
-            }
-        }
-        return HttpResult.error(HttpStatus.PARAMETERS_PATTERN_ERROR_CODE, sb.toString());
-    }
-}