RRExceptionHandler.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * Copyright (c) 2016-2019 人人开源 All rights reserved.
  3. *
  4. * https://www.renren.io
  5. *
  6. * 版权所有,侵权必究!
  7. */
  8. package com.huimv.video.dhicc.result;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import org.springframework.dao.DataIntegrityViolationException;
  12. import org.springframework.dao.DuplicateKeyException;
  13. import org.springframework.validation.FieldError;
  14. import org.springframework.validation.ObjectError;
  15. import org.springframework.web.HttpRequestMethodNotSupportedException;
  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.RestControllerAdvice;
  20. import org.springframework.web.servlet.NoHandlerFoundException;
  21. /**
  22. * 异常处理器
  23. *
  24. * @author Mark sunlightcs@gmail.com
  25. */
  26. @RestControllerAdvice
  27. public class RRExceptionHandler {
  28. private Logger logger = LoggerFactory.getLogger(getClass());
  29. /**
  30. * 处理自定义异常
  31. */
  32. @ExceptionHandler(RRException.class)
  33. public R handleRRException(RRException e){
  34. R r = new R();
  35. r.put("code", e.getCode());
  36. r.put("msg", e.getMessage());
  37. logger.error(e.getMsg(),e);
  38. return r;
  39. }
  40. @ExceptionHandler(NoHandlerFoundException.class)
  41. public R handlerNoFoundException(Exception e) {
  42. logger.error(e.getMessage(), e);
  43. return R.error(404, "路径不存在,请检查路径是否正确");
  44. }
  45. @ExceptionHandler(DuplicateKeyException.class)
  46. public R handleDuplicateKeyException(DuplicateKeyException e){
  47. logger.error(e.toString());
  48. String message = e.getCause().getMessage();
  49. String[] s = message.split(" ");
  50. return R.error(600,s[2].replace("'","") + " 已经存在!");
  51. }
  52. // @ExceptionHandler(AuthorizationException.class)
  53. // public R handleAuthorizationException(AuthorizationException e){
  54. // logger.error(e.getMessage(), e);
  55. // return R.error("没有权限,请联系管理员授权");
  56. // }
  57. //
  58. // @ExceptionHandler(value = LimitAccessException.class)
  59. // public R handleLimitAccessException(LimitAccessException e) {
  60. // logger.error("LimitAccessException", e);
  61. // return R.error(429,e.getMessage());
  62. // }
  63. //当校验失败时,会抛出MethodArgumentNotValidException
  64. //输出错误原因到前端
  65. @ExceptionHandler(MethodArgumentNotValidException.class)
  66. public R handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
  67. logger.error(ex.getMessage(),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(DataIntegrityViolationException.class)
  90. public R handDataIntegrityViolationException(DataIntegrityViolationException e) {
  91. logger.error(e.getMessage(),e);
  92. return R.error(1001,e.getCause().getMessage());
  93. }
  94. @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
  95. public R handHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
  96. logger.error(e.getMessage(), e);
  97. return R.error(1001, e.getMessage());
  98. }
  99. @ExceptionHandler(Exception.class)
  100. public R handleException(Exception e){
  101. logger.error(e.getMessage(), e);
  102. return R.error();
  103. }
  104. }