GlobalExceptionHandler.java 3.95 KB
package com.glsx.api.exception;

import java.util.List;
import java.util.concurrent.TimeoutException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.core.web.R;
import com.netflix.hystrix.exception.HystrixRuntimeException;
import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * <p>Description:
 * <p>Project: chejia-smart
 *
 * @author wbh
 * @date 2021-03-16
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler({BusinessException.class})
    public R handleBusinessException(BusinessException e) {
        log.error("!!!! 业务异常", e);
        return R.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler({ServletRequestBindingException.class, MethodArgumentNotValidException.class,
            BindException.class})
    public R handleBindException(Exception e) {
        log.error("!!!! 参数错误", e);
        if ((e instanceof MethodArgumentNotValidException) || (e instanceof BindException)) {
            List<FieldError> errorList;
            if (e instanceof MethodArgumentNotValidException) {
                MethodArgumentNotValidException ex = (MethodArgumentNotValidException) e;
                errorList = ex.getBindingResult().getFieldErrors();
            } else {
                BindException ex = (BindException) e;
                errorList = ex.getFieldErrors();
            }
            StringBuffer messageSb = new StringBuffer();
            for (FieldError error : errorList) {
                String fieldMessage = String.format("%s: %s, ",
                        error.getField(), error.getDefaultMessage());
                messageSb.append(fieldMessage);
            }
            return R.error(ResultEnum.PARAM_ERROR.getCode(),
                    messageSb.toString().replaceAll(", +$", ""));
        } else {
            return R.error(ResultEnum.PARAM_ERROR.getCode(), ResultEnum.PARAM_ERROR.getMessage());
        }
    }

    @ExceptionHandler({HystrixRuntimeException.class})
    public R handleHystrixException(HystrixRuntimeException e) {
        Throwable cause = e.getCause();
        if (cause instanceof FeignException) {
            FeignException feignException = (FeignException) cause;
            return this.handleFeignException(feignException);
        } else if (cause instanceof TimeoutException) {
            log.error("!!!! 服务调用超时", e);
            return R.error(ResultEnum.REMOTE_SERVER_TIME_OUT.getCode(),
                    ResultEnum.REMOTE_SERVER_TIME_OUT.getMessage());
        } else {
            log.error("!!!! Hystrix熔断错误", e);
            return R.error(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMessage());
        }
    }

    @ExceptionHandler({FeignException.class})
    public R handleFeignException(FeignException e) {
        log.error("!!!! 远程服务错误", e);
        String respMessage;
        if (StringUtils.isNotBlank(e.contentUTF8())) {
            JSONObject respObj = JSON.parseObject(e.contentUTF8());
            respMessage = respObj.getString("message");
        } else {
            respMessage = "";
        }
        int code = ResultEnum.REMOTE_SERVICE_ERROR.getCode();
        String message = StringUtils.isBlank(respMessage)
                ? ResultEnum.REMOTE_SERVICE_ERROR.getMessage() : respMessage;
        return R.error(code, message);
    }

    @ExceptionHandler(Exception.class)
    public R handleException(Exception e) {
        log.error("!!!! 未知异常", e);
        return R.error(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMessage());
    }
}