GlobalExceptionHandler.java 1.19 KB
package com.glsx.smart.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
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 String handleBusinessException(BusinessException e) {
        log.error("!!!! 业务异常", e);
        return e.getMessage();
    }

    @ExceptionHandler({ServletRequestBindingException.class, MethodArgumentNotValidException.class,
            BindException.class})
    public String handleBindException(Exception e) {
        log.error("!!!! 参数错误", e);
        return ResultEnum.PARAM_ERROR.getMessage();
    }

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