DefaultExceptionHandler.java 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package com.ruoyi.framework.web.exception;
  2. import org.apache.shiro.authz.AuthorizationException;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.web.HttpRequestMethodNotSupportedException;
  6. import org.springframework.web.bind.annotation.ExceptionHandler;
  7. import org.springframework.web.bind.annotation.RestControllerAdvice;
  8. import com.ruoyi.common.exception.DemoModeException;
  9. import com.ruoyi.common.utils.security.PermissionUtils;
  10. import com.ruoyi.framework.web.domain.AjaxResult;
  11. /**
  12. * 自定义异常处理器
  13. *
  14. * @author ruoyi
  15. */
  16. @RestControllerAdvice
  17. public class DefaultExceptionHandler
  18. {
  19. private static final Logger log = LoggerFactory.getLogger(DefaultExceptionHandler.class);
  20. /**
  21. * 权限校验失败
  22. */
  23. @ExceptionHandler(AuthorizationException.class)
  24. public AjaxResult handleAuthorizationException(AuthorizationException e)
  25. {
  26. log.error(e.getMessage(), e);
  27. return AjaxResult.error(PermissionUtils.getMsg(e.getMessage()));
  28. }
  29. /**
  30. * 请求方式不支持
  31. */
  32. @ExceptionHandler({ HttpRequestMethodNotSupportedException.class })
  33. public AjaxResult handleException(HttpRequestMethodNotSupportedException e)
  34. {
  35. log.error(e.getMessage(), e);
  36. return AjaxResult.error("不支持' " + e.getMethod() + "'请求");
  37. }
  38. /**
  39. * 拦截未知的运行时异常
  40. */
  41. @ExceptionHandler(RuntimeException.class)
  42. public AjaxResult notFount(RuntimeException e)
  43. {
  44. log.error("运行时异常:", e);
  45. return AjaxResult.error("运行时异常:" + e.getMessage());
  46. }
  47. /**
  48. * 系统异常
  49. */
  50. @ExceptionHandler(Exception.class)
  51. public AjaxResult handleException(Exception e)
  52. {
  53. log.error(e.getMessage(), e);
  54. return AjaxResult.error("服务器错误,请联系管理员");
  55. }
  56. /**
  57. * 演示模式异常
  58. */
  59. @ExceptionHandler(DemoModeException.class)
  60. public AjaxResult demoModeException(DemoModeException e)
  61. {
  62. return AjaxResult.error("演示模式,不允许操作");
  63. }
  64. }