| | |
| | | package com.ruoyi.framework.web.exception; |
| | | |
| | | import cn.dev33.satoken.exception.NotLoginException; |
| | | import cn.dev33.satoken.exception.NotPermissionException; |
| | | import cn.dev33.satoken.exception.NotRoleException; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.http.HttpStatus; |
| | | import com.ruoyi.common.core.domain.AjaxResult; |
| | | import com.ruoyi.common.exception.BaseException; |
| | | import com.ruoyi.common.exception.CustomException; |
| | | import com.ruoyi.common.core.domain.R; |
| | | import com.ruoyi.common.exception.DemoModeException; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.security.access.AccessDeniedException; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.common.utils.StreamUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.mybatis.spring.MyBatisSystemException; |
| | | import org.springframework.context.support.DefaultMessageSourceResolvable; |
| | | import org.springframework.dao.DuplicateKeyException; |
| | | import org.springframework.validation.BindException; |
| | | import org.springframework.web.HttpRequestMethodNotSupportedException; |
| | | import org.springframework.web.bind.MethodArgumentNotValidException; |
| | | import org.springframework.web.bind.annotation.ExceptionHandler; |
| | | import org.springframework.web.bind.annotation.RestControllerAdvice; |
| | | import org.springframework.web.servlet.NoHandlerFoundException; |
| | | |
| | | import javax.validation.ConstraintViolationException; |
| | | import jakarta.servlet.http.HttpServletRequest; |
| | | import jakarta.validation.ConstraintViolation; |
| | | import jakarta.validation.ConstraintViolationException; |
| | | |
| | | /** |
| | | * 全局异常处理器 |
| | | * |
| | | * @author ruoyi |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | @RestControllerAdvice |
| | | public class GlobalExceptionHandler |
| | | { |
| | | private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); |
| | | public class GlobalExceptionHandler { |
| | | |
| | | /** |
| | | * 权限校验异常 |
| | | * 权限码异常 |
| | | */ |
| | | @ExceptionHandler(AccessDeniedException.class) |
| | | public AjaxResult handleAccessDeniedException(AccessDeniedException e, HttpServletRequest request) |
| | | { |
| | | @ExceptionHandler(NotPermissionException.class) |
| | | public R<Void> handleNotPermissionException(NotPermissionException e, HttpServletRequest request) { |
| | | String requestURI = request.getRequestURI(); |
| | | log.error("请求地址'{}',权限校验失败'{}'", requestURI, e.getMessage()); |
| | | return AjaxResult.error(HttpStatus.HTTP_FORBIDDEN, "没有权限,请联系管理员授权"); |
| | | log.error("请求地址'{}',权限码校验失败'{}'", requestURI, e.getMessage()); |
| | | return R.fail(HttpStatus.HTTP_FORBIDDEN, "没有访问权限,请联系管理员授权"); |
| | | } |
| | | |
| | | /** |
| | | * 角色权限异常 |
| | | */ |
| | | @ExceptionHandler(NotRoleException.class) |
| | | public R<Void> handleNotRoleException(NotRoleException e, HttpServletRequest request) { |
| | | String requestURI = request.getRequestURI(); |
| | | log.error("请求地址'{}',角色权限校验失败'{}'", requestURI, e.getMessage()); |
| | | return R.fail(HttpStatus.HTTP_FORBIDDEN, "没有访问权限,请联系管理员授权"); |
| | | } |
| | | |
| | | /** |
| | | * 认证失败 |
| | | */ |
| | | @ExceptionHandler(NotLoginException.class) |
| | | public R<Void> handleNotLoginException(NotLoginException e, HttpServletRequest request) { |
| | | String requestURI = request.getRequestURI(); |
| | | log.error("请求地址'{}',认证失败'{}',无法访问系统资源", requestURI, e.getMessage()); |
| | | return R.fail(HttpStatus.HTTP_UNAUTHORIZED, "认证失败,无法访问系统资源"); |
| | | } |
| | | |
| | | /** |
| | | * 请求方式不支持 |
| | | */ |
| | | @ExceptionHandler(HttpRequestMethodNotSupportedException.class) |
| | | public AjaxResult handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, |
| | | HttpServletRequest request) |
| | | { |
| | | public R<Void> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, |
| | | HttpServletRequest request) { |
| | | String requestURI = request.getRequestURI(); |
| | | log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod()); |
| | | return AjaxResult.error(e.getMessage()); |
| | | return R.fail(e.getMessage()); |
| | | } |
| | | |
| | | /** |
| | | * 主键或UNIQUE索引,数据重复异常 |
| | | */ |
| | | @ExceptionHandler(DuplicateKeyException.class) |
| | | public R<Void> handleDuplicateKeyException(DuplicateKeyException e, HttpServletRequest request) { |
| | | String requestURI = request.getRequestURI(); |
| | | log.error("请求地址'{}',数据库中已存在记录'{}'", requestURI, e.getMessage()); |
| | | return R.fail("数据库中已存在该记录,请联系管理员确认"); |
| | | } |
| | | |
| | | /** |
| | | * Mybatis系统异常 通用处理 |
| | | */ |
| | | @ExceptionHandler(MyBatisSystemException.class) |
| | | public R<Void> handleCannotFindDataSourceException(MyBatisSystemException e, HttpServletRequest request) { |
| | | String requestURI = request.getRequestURI(); |
| | | String message = e.getMessage(); |
| | | if (message.contains("CannotFindDataSourceException")) { |
| | | log.error("请求地址'{}', 未找到数据源", requestURI); |
| | | return R.fail("未找到数据源,请联系管理员确认"); |
| | | } |
| | | log.error("请求地址'{}', Mybatis系统异常", requestURI, e); |
| | | return R.fail(message); |
| | | } |
| | | |
| | | /** |
| | | * 业务异常 |
| | | */ |
| | | @ExceptionHandler(ServiceException.class) |
| | | public AjaxResult handleServiceException(ServiceException e, HttpServletRequest request) |
| | | { |
| | | public R<Void> handleServiceException(ServiceException e, HttpServletRequest request) { |
| | | log.error(e.getMessage(), e); |
| | | Integer code = e.getCode(); |
| | | return StringUtils.isNotNull(code) ? AjaxResult.error(code, e.getMessage()) : AjaxResult.error(e.getMessage()); |
| | | return ObjectUtil.isNotNull(code) ? R.fail(code, e.getMessage()) : R.fail(e.getMessage()); |
| | | } |
| | | |
| | | /** |
| | | * 拦截未知的运行时异常 |
| | | */ |
| | | @ExceptionHandler(RuntimeException.class) |
| | | public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request) |
| | | { |
| | | public R<Void> handleRuntimeException(RuntimeException e, HttpServletRequest request) { |
| | | String requestURI = request.getRequestURI(); |
| | | log.error("请求地址'{}',发生未知异常.", requestURI, e); |
| | | return AjaxResult.error(e.getMessage()); |
| | | return R.fail(e.getMessage()); |
| | | } |
| | | |
| | | /** |
| | | * 系统异常 |
| | | */ |
| | | @ExceptionHandler(Exception.class) |
| | | public AjaxResult handleException(Exception e, HttpServletRequest request) |
| | | { |
| | | public R<Void> handleException(Exception e, HttpServletRequest request) { |
| | | String requestURI = request.getRequestURI(); |
| | | log.error("请求地址'{}',发生系统异常.", requestURI, e); |
| | | return AjaxResult.error(e.getMessage()); |
| | | return R.fail(e.getMessage()); |
| | | } |
| | | |
| | | /** |
| | | * 自定义验证异常 |
| | | */ |
| | | @ExceptionHandler(BindException.class) |
| | | public AjaxResult handleBindException(BindException e) |
| | | { |
| | | public R<Void> handleBindException(BindException e) { |
| | | log.error(e.getMessage(), e); |
| | | String message = e.getAllErrors().get(0).getDefaultMessage(); |
| | | return AjaxResult.error(message); |
| | | String message = StreamUtils.join(e.getAllErrors(), DefaultMessageSourceResolvable::getDefaultMessage, ", "); |
| | | return R.fail(message); |
| | | } |
| | | |
| | | /** |
| | | * 自定义验证异常 |
| | | */ |
| | | @ExceptionHandler(ConstraintViolationException.class) |
| | | public AjaxResult constraintViolationException(ConstraintViolationException e) { |
| | | public R<Void> constraintViolationException(ConstraintViolationException e) { |
| | | log.error(e.getMessage(), e); |
| | | String message = e.getConstraintViolations().iterator().next().getMessage(); |
| | | return AjaxResult.error(message); |
| | | String message = StreamUtils.join(e.getConstraintViolations(), ConstraintViolation::getMessage, ", "); |
| | | return R.fail(message); |
| | | } |
| | | |
| | | /** |
| | | * 自定义验证异常 |
| | | */ |
| | | @ExceptionHandler(MethodArgumentNotValidException.class) |
| | | public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e) |
| | | { |
| | | public R<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { |
| | | log.error(e.getMessage(), e); |
| | | String message = e.getBindingResult().getFieldError().getDefaultMessage(); |
| | | return AjaxResult.error(message); |
| | | return R.fail(message); |
| | | } |
| | | |
| | | /** |
| | | * 演示模式异常 |
| | | */ |
| | | @ExceptionHandler(DemoModeException.class) |
| | | public AjaxResult handleDemoModeException(DemoModeException e) |
| | | { |
| | | return AjaxResult.error("演示模式,不允许操作"); |
| | | public R<Void> handleDemoModeException(DemoModeException e) { |
| | | return R.fail("演示模式,不允许操作"); |
| | | } |
| | | } |