importcom.yzf.enterprise.market.common.constant.HttpStatus;importcom.yzf.enterprise.market.common.exception.BaseException;importcom.yzf.enterprise.market.common.exception.CustomException;importcom.yzf.enterprise.market.common.exception.DemoModeException;importcom.yzf.enterprise.market.common.utils.StringUtils;importcom.yzf.enterprise.market.framework.web.domain.AjaxResult;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.security.access.AccessDeniedException;importorg.springframework.security.authentication.AccountExpiredException;importorg.springframework.security.core.userdetails.UsernameNotFoundException;importorg.springframework.validation.BindException;importorg.springframework.web.bind.MethodArgumentNotValidException;importorg.springframework.web.bind.annotation.ExceptionHandler;importorg.springframework.web.bind.annotation.RestControllerAdvice;importorg.springframework.web.servlet.NoHandlerFoundException;/*** 全局异常处理器*/@RestControllerAdvicepublic classGlobalExceptionHandler {private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);/*** 基础异常*/@ExceptionHandler(BaseException.class)publicAjaxResult baseException(BaseException e) {returnAjaxResult.error(e.getMessage());
}/*** 业务异常*/@ExceptionHandler(CustomException.class)publicAjaxResult businessException(CustomException e) {if(StringUtils.isNull(e.getCode())) {returnAjaxResult.error(e.getMessage());
}returnAjaxResult.error(e.getCode(), e.getMessage());
}
@ExceptionHandler(NoHandlerFoundException.class)publicAjaxResult handlerNoFoundException(Exception e) {
log.error(e.getMessage(), e);return AjaxResult.error(HttpStatus.NOT_FOUND, "路径不存在,请检查路径是否正确");
}
@ExceptionHandler(AccessDeniedException.class)publicAjaxResult handleAuthorizationException(AccessDeniedException e) {
log.error(e.getMessage());return AjaxResult.error(HttpStatus.FORBIDDEN, "没有权限,请联系管理员授权");
}
}