JeecgBootExceptionHandler.java 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package org.jeecg.common.exception;
  2. import org.apache.shiro.authz.AuthorizationException;
  3. import org.apache.shiro.authz.UnauthorizedException;
  4. import org.jeecg.common.api.vo.Result;
  5. import org.springframework.dao.DuplicateKeyException;
  6. import org.springframework.web.HttpRequestMethodNotSupportedException;
  7. import org.springframework.web.bind.annotation.ExceptionHandler;
  8. import org.springframework.web.bind.annotation.RestControllerAdvice;
  9. import org.springframework.web.servlet.NoHandlerFoundException;
  10. import lombok.extern.slf4j.Slf4j;
  11. /**
  12. * 异常处理器
  13. *
  14. * @Author scott
  15. * @Date 2019
  16. */
  17. @RestControllerAdvice
  18. @Slf4j
  19. public class JeecgBootExceptionHandler {
  20. /**
  21. * 处理自定义异常
  22. */
  23. @ExceptionHandler(JeecgBootException.class)
  24. public Result<?> handleRRException(JeecgBootException e){
  25. log.error(e.getMessage(), e);
  26. return Result.error(e.getMessage());
  27. }
  28. @ExceptionHandler(NoHandlerFoundException.class)
  29. public Result<?> handlerNoFoundException(Exception e) {
  30. log.error(e.getMessage(), e);
  31. return Result.error(404, "路径不存在,请检查路径是否正确");
  32. }
  33. @ExceptionHandler(DuplicateKeyException.class)
  34. public Result<?> handleDuplicateKeyException(DuplicateKeyException e){
  35. log.error(e.getMessage(), e);
  36. return Result.error("数据库中已存在该记录");
  37. }
  38. @ExceptionHandler({UnauthorizedException.class, AuthorizationException.class})
  39. public Result<?> handleAuthorizationException(AuthorizationException e){
  40. log.error(e.getMessage(), e);
  41. return Result.noauth("没有权限,请联系管理员授权");
  42. }
  43. @ExceptionHandler(Exception.class)
  44. public Result<?> handleException(Exception e){
  45. log.error(e.getMessage(), e);
  46. return Result.error(e.getMessage());
  47. }
  48. /**
  49. * @Author 政辉
  50. * @param e
  51. * @return
  52. */
  53. @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
  54. public Result<?> HttpRequestMethodNotSupportedException(Exception e){
  55. log.error(e.getMessage(), e);
  56. return Result.error("没有权限,请联系管理员授权");
  57. }
  58. }