GlobalExceptionHandler.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package com.izouma.awesomeAdmin.exception;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.springframework.http.HttpStatus;
  4. import org.springframework.transaction.TransactionSystemException;
  5. import org.springframework.validation.BindException;
  6. import org.springframework.validation.FieldError;
  7. import org.springframework.validation.ObjectError;
  8. import org.springframework.web.bind.MethodArgumentNotValidException;
  9. import org.springframework.web.bind.annotation.ControllerAdvice;
  10. import org.springframework.web.bind.annotation.ExceptionHandler;
  11. import org.springframework.web.bind.annotation.ResponseBody;
  12. import org.springframework.web.bind.annotation.ResponseStatus;
  13. import javax.servlet.http.HttpServletRequest;
  14. import javax.validation.ConstraintViolation;
  15. import javax.validation.ConstraintViolationException;
  16. import javax.validation.Path;
  17. import java.util.HashMap;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.stream.Collectors;
  22. @ControllerAdvice
  23. @Slf4j
  24. public class GlobalExceptionHandler {
  25. @ExceptionHandler(value = BusinessException.class)
  26. @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  27. @ResponseBody
  28. public Map<String, Object> serviceExceptionHandler(BusinessException e) {
  29. Map<String, Object> map = new HashMap<>();
  30. map.put("error", e.getError());
  31. map.put("message", e.getMessage());
  32. map.put("code", -1);
  33. return map;
  34. }
  35. @ExceptionHandler({AuthenticationException.class})
  36. @ResponseStatus(HttpStatus.UNAUTHORIZED)
  37. @ResponseBody
  38. public Map<String, Object> handleAuthenticationException(AuthenticationException e) {
  39. Map<String, Object> map = new HashMap<>();
  40. map.put("error", e.getMessage());
  41. map.put("code", -1);
  42. return map;
  43. }
  44. @ExceptionHandler(value = TransactionSystemException.class)
  45. @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  46. @ResponseBody
  47. public Map<String, Object> serviceExceptionHandler(TransactionSystemException e) {
  48. String message = e.getMessage();
  49. try {
  50. if (e.getCause().getCause() instanceof ConstraintViolationException) {
  51. ConstraintViolationException violationException = (ConstraintViolationException) e.getCause().getCause();
  52. message = violationException.getConstraintViolations().stream()
  53. .map(constraintViolation -> constraintViolation.getPropertyPath() + constraintViolation.getMessage())
  54. .collect(Collectors.joining(","));
  55. log.error(message);
  56. }
  57. } catch (Exception ignore) {
  58. }
  59. Map<String, Object> map = new HashMap<>();
  60. map.put("error", message);
  61. map.put("code", -1);
  62. return map;
  63. }
  64. @ExceptionHandler(value = Exception.class)
  65. @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  66. @ResponseBody
  67. public Map<String, Object> serviceExceptionHandler(Exception e) {
  68. Map<String, Object> map = new HashMap<>();
  69. map.put("error", e.getMessage());
  70. map.put("code", -1);
  71. log.error(e.getMessage(), e);
  72. return map;
  73. }
  74. @ExceptionHandler({BindException.class, ConstraintViolationException.class, MethodArgumentNotValidException.class})
  75. @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  76. @ResponseBody
  77. public Map<String, Object> validateExceptionHandler(Exception e, HttpServletRequest request) {
  78. log.error("请求:{}发生异常:{}", request.getRequestURI(), e);
  79. // 错误信息
  80. StringBuilder sb = new StringBuilder("参数校验失败:");
  81. // 错误信息map
  82. Map<String, String> error = new HashMap<>();
  83. String msg = "";
  84. if (!(e instanceof BindException) && !(e instanceof MethodArgumentNotValidException)) {
  85. for (ConstraintViolation cv : ((ConstraintViolationException) e).getConstraintViolations()) {
  86. msg = cv.getMessage();
  87. sb.append(msg).append(";");
  88. Iterator<Path.Node> it = cv.getPropertyPath().iterator();
  89. Path.Node last = null;
  90. while (it.hasNext()) {
  91. last = (Path.Node) it.next();
  92. }
  93. /*for(last = null; it.hasNext(); last = (Path.Node)it.next()) {
  94. }*/
  95. error.put(last != null ? last.getName() : "", msg);
  96. }
  97. } else {
  98. List<ObjectError> allErrors = null;
  99. if (e instanceof BindException) {
  100. allErrors = ((BindException) e).getAllErrors();
  101. } else {
  102. allErrors = ((MethodArgumentNotValidException) e).getBindingResult().getAllErrors();
  103. }
  104. // 拼接错误信息
  105. for (ObjectError oe : allErrors) {
  106. msg = oe.getDefaultMessage();
  107. sb.append(msg).append(";");
  108. if (oe instanceof FieldError) {
  109. error.put(((FieldError) oe).getField(), msg);
  110. } else {
  111. error.put(oe.getObjectName(), msg);
  112. }
  113. }
  114. }
  115. Map<String, Object> map = new HashMap<>();
  116. map.put("error", "参数校验失败");
  117. map.put("message", sb);
  118. map.put("code", -1);
  119. log.error(e.getMessage());
  120. return map;
  121. }
  122. }