package com.sziov.common.config;
import com.sziov.common.constant.CommonErrorCodeEnum;
import com.sziov.common.entity.ResultVo;
import com.sziov.common.exception.BusinessException;
import com.sziov.common.exception.CommonException;
import com.sziov.common.utils.DateUtils;
import com.sziov.common.utils.ResultUtils;
import java.beans.PropertyEditorSupport;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* 全局异常拦截
*/
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
public GlobalExceptionHandler() {
}
@ResponseBody
@ExceptionHandler({BusinessException.class})
public ResultVo errorHandlerBusinessException(BusinessException ex) {
log.error("BusinessException,code:" + ex.getErrorCode() + ",msg:" + ex.getMessage());
return ResultUtils.error(ex.getErrorCode(), ex.getMessage());
}
@ResponseBody
@ExceptionHandler({CommonException.class})
public ResultVo errorHandlerBusinessException(CommonException ex) {
log.error("CommonException,code:" + ex.getErrorCode() + ",msg:" + ex.getMessage());
return ResultUtils.error(ex.getErrorCode(), ex.getMessage());
}
@ResponseBody
@ExceptionHandler({Exception.class})
public ResultVo errorHandlerException(Exception ex) {
log.error("Exception,code:" + CommonErrorCodeEnum.ERROR_CODE_UNKNOWN.getCode() + ",msg:" + CommonErrorCodeEnum.ERROR_CODE_UNKNOWN.getMessage());
log.error(CommonErrorCodeEnum.ERROR_CODE_UNKNOWN.getMessage(), ex);
return ResultUtils.error(CommonErrorCodeEnum.ERROR_CODE_UNKNOWN.getCode(), CommonErrorCodeEnum.ERROR_CODE_UNKNOWN.getMessage());
}
}
|