RRExceptionHandler.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.common.exception;
  2. import com.alibaba.fastjson.JSONException;
  3. import com.common.utils.LogUtils;
  4. import com.common.utils.R;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.dao.DuplicateKeyException;
  8. import org.springframework.http.converter.HttpMessageConversionException;
  9. import org.springframework.http.converter.HttpMessageNotReadableException;
  10. import org.springframework.validation.FieldError;
  11. import org.springframework.web.HttpRequestMethodNotSupportedException;
  12. import org.springframework.web.bind.MethodArgumentNotValidException;
  13. import org.springframework.web.bind.MissingServletRequestParameterException;
  14. import org.springframework.web.bind.annotation.ExceptionHandler;
  15. import org.springframework.web.bind.annotation.RestControllerAdvice;
  16. import org.springframework.web.servlet.NoHandlerFoundException;
  17. @Slf4j
  18. @RestControllerAdvice
  19. public class RRExceptionHandler {
  20. @Autowired
  21. private LogUtils logUtils;
  22. /**
  23. * 处理自定义异常
  24. */
  25. @ExceptionHandler(RRException.class)
  26. public R handleRRException(RRException e){
  27. R r;
  28. // if (StringUtils.isEmpty(e.getMessage())) {
  29. // r = R.error();
  30. // }
  31. r = R.error(e.getErrno(), e.getErrmsg());
  32. logUtils.requestLog(r);
  33. return r;
  34. }
  35. @ExceptionHandler(NoHandlerFoundException.class)
  36. public R handlerMyNoHandlerFoundException(NoHandlerFoundException e) {
  37. R r = R.error("404", "路径不存在,请检查路径是否正确");
  38. logUtils.requestLog(r);
  39. return r;
  40. }
  41. @ExceptionHandler(DuplicateKeyException.class)
  42. public R handleDuplicateKeyException(DuplicateKeyException e){
  43. logUtils.requestLog(e);
  44. return R.error("数据库中已存在该记录");
  45. }
  46. // @ExceptionHandler(AuthorizationException.class)
  47. // public R handleAuthorizationException(AuthorizationException e){
  48. // R r = R.error("没有权限,请联系管理员授权");
  49. // logUtils.requestLog(r);
  50. // return r;
  51. // }
  52. /**
  53. * 处理 @Valid 对 @RequestBody 参数验证异常
  54. * @param e
  55. * @return
  56. */
  57. @ExceptionHandler(MethodArgumentNotValidException.class)
  58. public R handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
  59. FieldError fieldError = e.getBindingResult().getFieldError();
  60. R r = R.error(fieldError.getField() + fieldError.getDefaultMessage());
  61. logUtils.requestLog(r);
  62. return r;
  63. }
  64. /**
  65. * 参数类型转换错误
  66. *
  67. * @param exception 错误
  68. * @return 错误信息
  69. */
  70. @ExceptionHandler(HttpMessageConversionException.class)
  71. public R parameterTypeException(HttpMessageConversionException exception) {
  72. R r = R.error(exception.getCause().getLocalizedMessage());
  73. return r;
  74. }
  75. /**
  76. * 处理 @RequestBody 但 body none 异常
  77. * @param e
  78. * @return
  79. */
  80. @ExceptionHandler(HttpMessageNotReadableException.class)
  81. public R handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
  82. String message = e.getMessage();
  83. R r = R.error(message.substring(0, message.indexOf(":")));
  84. logUtils.requestLog(message);
  85. return r;
  86. }
  87. /**
  88. * 处理 @RequestParam required=true 但未收到值异常
  89. * @param e
  90. * @return
  91. */
  92. @ExceptionHandler(MissingServletRequestParameterException.class)
  93. public R handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
  94. R r = R.error("缺少参数" + e.getParameterName());
  95. logUtils.requestLog(r);
  96. return r;
  97. }
  98. @ExceptionHandler(JSONException.class)
  99. public R handleJSONException(JSONException e){
  100. // logUtils.requestLog(e);
  101. return R.error("数据格式错误");
  102. }
  103. /**
  104. * 处理请求方法不支持异常
  105. * @param e
  106. * @return
  107. */
  108. @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
  109. public R handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
  110. R r = R.error(String.format("当前路径不支持%s请求", e.getMethod()));
  111. logUtils.requestLog(r);
  112. return r;
  113. }
  114. @ExceptionHandler(Exception.class)
  115. public R handleException(Exception e){
  116. logUtils.requestLog(e);
  117. e.printStackTrace();
  118. return R.error();
  119. }
  120. }