RRExceptionHandler.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * Copyright (c) 2016-2019 人人开源 All rights reserved.
  3. *
  4. * https://www.renren.io
  5. *
  6. * 版权所有,侵权必究!
  7. */
  8. package com.huimv.common.exception;
  9. import com.huimv.common.utils.R;
  10. import org.apache.shiro.authz.AuthorizationException;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.dao.DuplicateKeyException;
  14. import org.springframework.validation.FieldError;
  15. import org.springframework.validation.ObjectError;
  16. import org.springframework.web.bind.MethodArgumentNotValidException;
  17. import org.springframework.web.bind.MissingServletRequestParameterException;
  18. import org.springframework.web.bind.annotation.ExceptionHandler;
  19. import org.springframework.web.bind.annotation.ResponseBody;
  20. import org.springframework.web.bind.annotation.RestControllerAdvice;
  21. import org.springframework.web.servlet.NoHandlerFoundException;
  22. import javax.validation.ConstraintViolationException;
  23. import java.sql.SQLIntegrityConstraintViolationException;
  24. /**
  25. * 异常处理器
  26. *
  27. * @author Mark sunlightcs@gmail.com
  28. */
  29. @RestControllerAdvice
  30. public class RRExceptionHandler {
  31. private Logger logger = LoggerFactory.getLogger(getClass());
  32. /**
  33. * 处理自定义异常
  34. */
  35. @ExceptionHandler(RRException.class)
  36. public R handleRRException(RRException e){
  37. R r = new R();
  38. r.put("code", e.getCode());
  39. r.put("msg", e.getMessage());
  40. return r;
  41. }
  42. @ExceptionHandler(NoHandlerFoundException.class)
  43. public R handlerNoFoundException(Exception e) {
  44. logger.error(e.getMessage(), e);
  45. return R.error(404, "路径不存在,请检查路径是否正确");
  46. }
  47. @ExceptionHandler(DuplicateKeyException.class)
  48. public R handleDuplicateKeyException(DuplicateKeyException e){
  49. logger.error(e.toString());
  50. String message = e.getCause().getMessage();
  51. String[] s = message.split(" ");
  52. return R.error(600,s[2].replace("'","") + " 已经存在!");
  53. }
  54. @ExceptionHandler(AuthorizationException.class)
  55. public R handleAuthorizationException(AuthorizationException e){
  56. logger.error(e.getMessage(), e);
  57. return R.error("没有权限,请联系管理员授权");
  58. }
  59. @ExceptionHandler(value = LimitAccessException.class)
  60. public R handleLimitAccessException(LimitAccessException e) {
  61. logger.error("LimitAccessException", e);
  62. return R.error(429,e.getMessage());
  63. }
  64. //当校验失败时,会抛出MethodArgumentNotValidException
  65. //输出错误原因到前端
  66. @ExceptionHandler(MethodArgumentNotValidException.class)
  67. public R handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
  68. StringBuilder sb = new StringBuilder();
  69. for(ObjectError error : ex.getBindingResult().getAllErrors()){
  70. if(error instanceof FieldError){
  71. FieldError e = (FieldError) error;
  72. sb.append(e.getField()).append(e.getDefaultMessage()).append(" ");
  73. }
  74. }
  75. return R.error(1001,sb.toString());
  76. }
  77. @ExceptionHandler(ConstraintViolationException.class)
  78. public R handleConstraintViolationException(ConstraintViolationException e) {
  79. logger.error(e.getMessage(),e);
  80. StringBuilder sb = new StringBuilder();
  81. e.getConstraintViolations().forEach(constraintViolation -> sb.append(constraintViolation.getMessage()).append(" "));
  82. return R.error(1001,sb.toString().substring(0,sb.length() - 1));
  83. }
  84. @ExceptionHandler(MissingServletRequestParameterException.class)
  85. public R handMissingServletRequestParameterException(MissingServletRequestParameterException e) {
  86. logger.error(e.getMessage(),e);
  87. return R.error(1001,e.getMessage());
  88. }
  89. @ExceptionHandler(Exception.class)
  90. public R handleException(Exception e){
  91. logger.error(e.getMessage(), e);
  92. return R.error();
  93. }
  94. }