GlobalExceptionHandler.java 4.78 KB
package com.gsc4cloud.gsc.facade.vpcminiapp.handler;

import com.gsc4cloud.gsc.common.core.util.R;
import com.gsc4cloud.gsc.vpc.api.enums.ErrorCodeEnum;
import com.gsc4cloud.gsc.vpc.api.exception.BusinessException;
import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Description 本地异常处理类
 * @Author yelq
 * @Date 2025/9/19-20:47
 * @Version 1.0
 */
@Order(9999)
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    public GlobalExceptionHandler() {
    }

    /**
     * 处理业务异常
     */
    @ExceptionHandler(BusinessException.class)
    public ResponseEntity<R<?>> handleBusinessException(BusinessException e) {
        log.error("业务异常: code={}, message={}", e.getCode(), e.getMessage(), e);
        return ResponseEntity
                .status(HttpStatus.OK)
                .body(R.restResult(null, e.getCode(), e.getMessage()));
    }

    /**
     * 参数校验异常统一处理
     */
    @ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
    public ResponseEntity<R<?>> handleValidationException(Exception e) {
        BindingResult bindingResult = null;
        if (e instanceof MethodArgumentNotValidException) {
            bindingResult = ((MethodArgumentNotValidException) e).getBindingResult();
        } else if (e instanceof BindException) {
            bindingResult = ((BindException) e).getBindingResult();
        }

        String message = Optional.ofNullable(bindingResult)
                .map(result -> result.getAllErrors().stream()
                        .map(ObjectError::getDefaultMessage)
                        .collect(Collectors.joining("; ")))
                .orElse("参数校验失败");

        log.error("参数校验异常: {}", message, e);

        return ResponseEntity
                .status(HttpStatus.OK)
                .body(R.restResult(null, ErrorCodeEnum.PARAM_INVALID.getCode(), message));
    }

    /**
     * 处理请求方法不支持异常
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResponseEntity<R<?>> handleHttpRequestMethodNotSupportedException(
            HttpRequestMethodNotSupportedException e) {
        log.error("请求方法不支持: {}", e.getMessage(), e);
        return ResponseEntity
                .status(HttpStatus.METHOD_NOT_ALLOWED)
                .body(R.restResult(null,405, "请求方法不支持"));
    }

    /**
     * 处理媒体类型不支持异常
     */
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public ResponseEntity<R<?>> handleHttpMediaTypeNotSupportedException(
            HttpMediaTypeNotSupportedException e) {
        log.error("媒体类型不支持: {}", e.getMessage(), e);
        return ResponseEntity
                .status(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
                .body(R.restResult(null,415, "媒体类型不支持"));
    }

    /**
     * 处理空指针异常
     */
    @ExceptionHandler(NullPointerException.class)
    public ResponseEntity<R<?>> handleNullPointerException(NullPointerException e) {
        log.error("空指针异常", e);
        return ResponseEntity
                .status(HttpStatus.OK)
                .body(R.restResult(null, ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMessage()));
    }

    /**
     * 处理其他所有异常
     */
    @ExceptionHandler(Exception.class)
    public ResponseEntity<R<?>> handleException(Exception e) {
        log.error("系统异常", e);
        return ResponseEntity
                .status(HttpStatus.OK)
                .body(R.restResult(null, ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMessage()));
    }

    /**
     * 处理Feign客户端异常
     */
    @ExceptionHandler(FeignException.class)
    public ResponseEntity<R<?>> handleFeignException(FeignException e) {
        log.error("Feign客户端异常: status={}, message={}", e.status(), e.getMessage(), e);
        return ResponseEntity
                .status(HttpStatus.OK)
                .body(R.restResult(null, ErrorCodeEnum.FEIGN_ERROR.getCode(),"服务调用失败: " + e.getMessage()));
    }
}