All Downloads are FREE. Search and download functionalities are using the official Maven repository.

leisure.springboot.web.exception.ExceptionAdvice Maven / Gradle / Ivy

The newest version!
package leisure.springboot.web.exception;

import leisure.core.common.Result;
import leisure.core.exception.BaseException;
import leisure.springboot.core.BeanFactory;
import leisure.springboot.core.CoreUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@ControllerAdvice
@ResponseBody
public class ExceptionAdvice {
    private static final Logger logger = LoggerFactory.getLogger(ExceptionAdvice.class);

//    private static final String profile = CoreUtils.CurrentActiveProfiles();//dev test rc prod

    final Map leisureExceptionhandlers;

    public ExceptionAdvice() {
        leisureExceptionhandlers = BeanFactory.getApplicationContext().getBeansOfType(LeisureExceptionhandler.class);
    }

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public Object MethodArgumentNotValidHandler(MethodArgumentNotValidException exception) {
        Result rb = new Result();
        StringBuilder sb = new StringBuilder();
        sb.append("参数错误:MethodArgumentNotValidException");
        sb.append(",方法名称:");
        sb.append(exception.getParameter().getMethod().getName());
        sb.append(",字段如下:");
        MethodArgumentNotValidException meException = exception;
        for (FieldError error : meException.getBindingResult().getFieldErrors()) {
            sb.append(error.getField());
            sb.append(":  ");
            sb.append(error.getDefaultMessage());
            sb.append(":  ");
        }
        String msg = sb.toString().trim();
        if (msg.endsWith(":")) {
            msg.substring(0, msg.length() - 1);
        }
        writeParamLog(rb, msg);
        return rb;
    }

    @ExceptionHandler(value = ConstraintViolationException.class)
    public Object MethodArgumentNotValidHandler1(ConstraintViolationException exception) {
        Result rb = new Result();
        StringBuilder sb = new StringBuilder();
        sb.append("参数错误:ConstraintViolationException-");
        List msgs = exception.getConstraintViolations().stream()
                .map(ConstraintViolation::getMessage)
                .collect(Collectors.toList());
        for (String msg : msgs) {
            sb.append(msg);
            sb.append(";");
        }
        String msg = sb.toString().trim();
        if (msg.endsWith(";")) {
            msg.substring(0, msg.length() - 1);
        }
        writeParamLog(rb, msg);
        return rb;
    }

    @ExceptionHandler
    public Object HandleException(HttpServletRequest request, Exception exception) {
        Result rb = new Result();
        if (exception instanceof BaseException) {
            BaseException be = (BaseException) exception;
            rb.setCode(be.getCode());
            rb.setInfo(be.getInfo());
            writeExceptionLog(rb, be);
        } else {
            writeExceptionLog(rb, exception);
        }
        String url = request.getRequestURI();
        if (StringUtils.isNotBlank(url)) {
            logger.error("错误请求地址:" + url);
        }

        StringBuilder sb = new StringBuilder();
        sb.append("错误请求地址:");
        sb.append(url);
        sb.append(" 错误信息:");
        sb.append(CoreUtils.ExceptionDetail(exception));
        logger.error(sb.toString());
        return rb;
    }

    private void writeExceptionLog(Result rb, Exception ex) {
        if (logger.isDebugEnabled()) {
            if (leisureExceptionhandlers != null && leisureExceptionhandlers.size() > 0) {
                for (LeisureExceptionhandler exceptionhandler : leisureExceptionhandlers.values()) {
                    rb = exceptionhandler.TestException(rb, ex);
                }
            }
        } else if (logger.isInfoEnabled()) {
            if (leisureExceptionhandlers != null && leisureExceptionhandlers.size() > 0) {
                for (LeisureExceptionhandler exceptionhandler : leisureExceptionhandlers.values()) {
                    rb = exceptionhandler.ProdException(rb, ex);
                }
            }
        }
    }

    private void writeParamLog(Result rb, String msg) {
        if (logger.isDebugEnabled()) {
            rb.setCode("40002");
            rb.setInfo(msg);
            logger.error(msg);
        } else {
            rb.setCode("40002");
            rb.setInfo("Api system error");
            logger.error("Api system error:");
            logger.error(msg);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy