统一异常处理

364 阅读1分钟

统一异常处理

/**
 *
 * @author gaozhiwei
 */
@RestControllerAdvice
public class GlobalControllerExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalControllerExceptionHandler.class);

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public RestResponse messageHandler(HttpServletRequest request, MethodArgumentNotValidException e) {
        String path = request.getRequestURL().toString();
        FieldError fieldError = e.getBindingResult().getFieldError();
        String error = null;
        if (fieldError != null) {
            error = e.getBindingResult().getFieldError().getDefaultMessage();
        }
        if (error == null) {
            error = ResponseEnum.PARAM_ERROR.getMessage();
        }
        logger.error("messageHandler服务错误,接口=[{}],错误原因=[{}]", path, e.getMessage());
        return RestResponse.error(ResponseEnum.PARAM_ERROR.getCode(), error);
    }

    @ExceptionHandler(value = Exception.class)
    public RestResponse exceptionHandler(HttpServletRequest request, Exception e) {
        String path = request.getRequestURL().toString();
        logger.error("exceptionHandler服务错误,接口=[{}],错误原因=[{}]", path, e.getMessage());
        e.printStackTrace();
        return RestResponse.error();
    }


    @ExceptionHandler(value = GlobalException.class)
    public RestResponse globalHandler(HttpServletRequest request, GlobalException e) {
        String path = request.getRequestURL().toString();
        logger.error("globalHandler预计中的错误,接口=[{}],错误信息=[{}]", path, e.getMessage());
        e.printStackTrace();
        return RestResponse.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    public RestResponse globalHandler(HttpServletRequest request, HttpRequestMethodNotSupportedException e) {
        String path = request.getRequestURL().toString();
        logger.error("HttpRequestMethodNotSupportedException错误,接口=[{}],错误信息=[{}]", path, e.getMessage());
        e.printStackTrace();
        return RestResponse.error(ResponseEnum.PARAM_ERROR.getCode(), e.getMessage());
    }

    @ExceptionHandler(value = InvalidFormatException.class)
    public RestResponse invalidFormatException(HttpServletRequest request, InvalidFormatException e) {
        String path = request.getRequestURL().toString();
        logger.error("InvalidFormatException,接口=[{}],错误信息=[{}]", path, e.getMessage());
        e.printStackTrace();
        return RestResponse.error(ResponseEnum.PARAM_ERROR.getCode(), "参数填写有误,请重新输入");
    }

    @ExceptionHandler(value = HttpMessageNotReadableException.class)
    public RestResponse httpMessageNotReadableException(HttpServletRequest request, HttpMessageNotReadableException e) {
        String path = request.getRequestURL().toString();
        logger.error("InvalidFormatException,接口=[{}],错误信息=[{}]", path, e.getMessage());
        e.printStackTrace();
        return RestResponse.error(ResponseEnum.PARAM_ERROR.getCode(), "参数填写有误,请重新输入");
    }

自定义异常类

/**
 * 全局异常类
 *
 * @author gzw
 * @date 2020/3/18
 */
public class GlobalException extends RuntimeException {

    private Integer code;

    private String message;

    public GlobalException(ResponseEnum responseEnum) {
        super(responseEnum.getMessage());
        this.code = responseEnum.getCode();
        this.message = responseEnum.getMessage();
    }

    public GlobalException(Integer code, String message) {
        super(message);
        this.code = code;
        this.message = message;
    }

    public GlobalException(Integer code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
        this.message = message;
    }

    public GlobalException(Throwable cause) {
        super(cause);
    }

    public GlobalException(Integer code, String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
        this.code = code;
        this.message = message;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }