9624dcd9b002bf62258c3c8d515a29ecc0f75a2f.svn-base 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package org.jeecg.common.exception;
  2. import io.lettuce.core.RedisConnectionException;
  3. import org.apache.shiro.authz.AuthorizationException;
  4. import org.apache.shiro.authz.UnauthorizedException;
  5. import org.jeecg.common.api.vo.Result;
  6. import org.springframework.dao.DataIntegrityViolationException;
  7. import org.springframework.dao.DuplicateKeyException;
  8. import org.springframework.data.redis.connection.PoolException;
  9. import org.springframework.web.HttpRequestMethodNotSupportedException;
  10. import org.springframework.web.bind.annotation.ExceptionHandler;
  11. import org.springframework.web.bind.annotation.RestControllerAdvice;
  12. import org.springframework.web.multipart.MaxUploadSizeExceededException;
  13. import org.springframework.web.servlet.NoHandlerFoundException;
  14. import lombok.extern.slf4j.Slf4j;
  15. /**
  16. * 异常处理器
  17. *
  18. * @Author scott
  19. * @Date 2019
  20. */
  21. @RestControllerAdvice
  22. @Slf4j
  23. public class JeecgBootExceptionHandler {
  24. /**
  25. * 处理自定义异常
  26. */
  27. @ExceptionHandler(JeecgBootException.class)
  28. public Result<?> handleRRException(JeecgBootException e){
  29. log.error(e.getMessage(), e);
  30. return Result.error(e.getMessage());
  31. }
  32. @ExceptionHandler(NoHandlerFoundException.class)
  33. public Result<?> handlerNoFoundException(Exception e) {
  34. log.error(e.getMessage(), e);
  35. return Result.error(404, "路径不存在,请检查路径是否正确");
  36. }
  37. @ExceptionHandler(DuplicateKeyException.class)
  38. public Result<?> handleDuplicateKeyException(DuplicateKeyException e){
  39. log.error(e.getMessage(), e);
  40. return Result.error("数据库中已存在该记录");
  41. }
  42. @ExceptionHandler({UnauthorizedException.class, AuthorizationException.class})
  43. public Result<?> handleAuthorizationException(AuthorizationException e){
  44. log.error(e.getMessage(), e);
  45. return Result.noauth("没有权限,请联系管理员授权");
  46. }
  47. @ExceptionHandler(Exception.class)
  48. public Result<?> handleException(Exception e){
  49. log.error(e.getMessage(), e);
  50. return Result.error("操作失败,"+e.getMessage());
  51. }
  52. /**
  53. * @Author 政辉
  54. * @param e
  55. * @return
  56. */
  57. @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
  58. public Result<?> HttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e){
  59. StringBuffer sb = new StringBuffer();
  60. sb.append("不支持");
  61. sb.append(e.getMethod());
  62. sb.append("请求方法,");
  63. sb.append("支持以下");
  64. String [] methods = e.getSupportedMethods();
  65. if(methods!=null){
  66. for(String str:methods){
  67. sb.append(str);
  68. sb.append("、");
  69. }
  70. }
  71. log.error(sb.toString(), e);
  72. //return Result.error("没有权限,请联系管理员授权");
  73. return Result.error(405,sb.toString());
  74. }
  75. /**
  76. * spring默认上传大小100MB 超出大小捕获异常MaxUploadSizeExceededException
  77. */
  78. @ExceptionHandler(MaxUploadSizeExceededException.class)
  79. public Result<?> handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e) {
  80. log.error(e.getMessage(), e);
  81. return Result.error("文件大小超出10MB限制, 请压缩或降低文件质量! ");
  82. }
  83. @ExceptionHandler(DataIntegrityViolationException.class)
  84. public Result<?> handleDataIntegrityViolationException(DataIntegrityViolationException e) {
  85. log.error(e.getMessage(), e);
  86. return Result.error("字段太长,超出数据库字段的长度");
  87. }
  88. @ExceptionHandler(PoolException.class)
  89. public Result<?> handlePoolException(PoolException e) {
  90. log.error(e.getMessage(), e);
  91. return Result.error("Redis 连接异常!");
  92. }
  93. }