BaseExceptionHandler.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package cc.mrbird.febs.common.handler;
  2. import cc.mrbird.febs.common.entity.FebsResponse;
  3. import cc.mrbird.febs.common.exception.FebsException;
  4. import cc.mrbird.febs.common.exception.FileDownloadException;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.apache.commons.lang3.StringUtils;
  7. import org.springframework.http.HttpStatus;
  8. import org.springframework.security.access.AccessDeniedException;
  9. import org.springframework.validation.BindException;
  10. import org.springframework.validation.FieldError;
  11. import org.springframework.web.HttpMediaTypeNotSupportedException;
  12. import org.springframework.web.HttpRequestMethodNotSupportedException;
  13. import org.springframework.web.bind.MethodArgumentNotValidException;
  14. import org.springframework.web.bind.annotation.ExceptionHandler;
  15. import org.springframework.web.bind.annotation.ResponseStatus;
  16. import javax.validation.ConstraintViolation;
  17. import javax.validation.ConstraintViolationException;
  18. import javax.validation.Path;
  19. import java.util.List;
  20. import java.util.Set;
  21. /**
  22. * @author MrBird
  23. */
  24. @Slf4j
  25. public class BaseExceptionHandler {
  26. @ExceptionHandler(value = Exception.class)
  27. @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  28. public FebsResponse handleException(Exception e) {
  29. log.error("系统内部异常,异常信息", e);
  30. return new FebsResponse().message("系统内部异常");
  31. }
  32. @ExceptionHandler(value = FebsException.class)
  33. @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  34. public FebsResponse handleFebsException(FebsException e) {
  35. log.error("系统错误", e);
  36. return new FebsResponse().message(e.getMessage());
  37. }
  38. /**
  39. * 统一处理请求参数校验(实体对象传参)
  40. *
  41. * @param e BindException
  42. * @return FebsResponse
  43. */
  44. @ExceptionHandler(BindException.class)
  45. @ResponseStatus(HttpStatus.BAD_REQUEST)
  46. public FebsResponse handleBindException(BindException e) {
  47. StringBuilder message = new StringBuilder();
  48. List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
  49. for (FieldError error : fieldErrors) {
  50. message.append(error.getField()).append(error.getDefaultMessage()).append(",");
  51. }
  52. message = new StringBuilder(message.substring(0, message.length() - 1));
  53. return new FebsResponse().message(message.toString());
  54. }
  55. /**
  56. * 统一处理请求参数校验(普通传参)
  57. *
  58. * @param e ConstraintViolationException
  59. * @return FebsResponse
  60. */
  61. @ExceptionHandler(value = ConstraintViolationException.class)
  62. @ResponseStatus(HttpStatus.BAD_REQUEST)
  63. public FebsResponse handleConstraintViolationException(ConstraintViolationException e) {
  64. StringBuilder message = new StringBuilder();
  65. Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
  66. for (ConstraintViolation<?> violation : violations) {
  67. Path path = violation.getPropertyPath();
  68. String[] pathArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(path.toString(), ".");
  69. message.append(pathArr[1]).append(violation.getMessage()).append(",");
  70. }
  71. message = new StringBuilder(message.substring(0, message.length() - 1));
  72. return new FebsResponse().message(message.toString());
  73. }
  74. /**
  75. * 统一处理请求参数校验(json)
  76. *
  77. * @param e ConstraintViolationException
  78. * @return FebsResponse
  79. */
  80. @ExceptionHandler(MethodArgumentNotValidException.class)
  81. @ResponseStatus(HttpStatus.BAD_REQUEST)
  82. public FebsResponse handlerMethodArgumentNotValidException(MethodArgumentNotValidException e) {
  83. StringBuilder message = new StringBuilder();
  84. for (FieldError error : e.getBindingResult().getFieldErrors()) {
  85. message.append(error.getField()).append(error.getDefaultMessage()).append(",");
  86. }
  87. message = new StringBuilder(message.substring(0, message.length() - 1));
  88. log.error(message.toString(), e);
  89. return new FebsResponse().message(message.toString());
  90. }
  91. @ExceptionHandler(value = FileDownloadException.class)
  92. @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  93. public void handleFileDownloadException(FileDownloadException e) {
  94. log.error("FileDownloadException", e);
  95. }
  96. @ExceptionHandler(value = AccessDeniedException.class)
  97. @ResponseStatus(HttpStatus.FORBIDDEN)
  98. public FebsResponse handleAccessDeniedException() {
  99. return new FebsResponse().message("没有权限访问该资源");
  100. }
  101. @ExceptionHandler(value = HttpMediaTypeNotSupportedException.class)
  102. @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  103. public FebsResponse handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
  104. return new FebsResponse().message("改方法不支持" + StringUtils.substringBetween(e.getMessage(), "'", "'") + "媒体类型");
  105. }
  106. @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
  107. @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  108. public FebsResponse handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
  109. return new FebsResponse().message("该方法不支持" + StringUtils.substringBetween(e.getMessage(), "'", "'") + "请求方法");
  110. }
  111. }