GlobalExceptionHandle.java 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package com.tzld.piaoquan.api.handle;
  2. import com.tzld.piaoquan.api.common.enums.ExceptionEnum;
  3. import com.tzld.piaoquan.api.common.exception.CommonException;
  4. import com.tzld.piaoquan.growth.common.common.base.CommonResponse;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import org.springframework.util.CollectionUtils;
  8. import org.springframework.validation.BindException;
  9. import org.springframework.validation.ObjectError;
  10. import org.springframework.web.bind.MethodArgumentNotValidException;
  11. import org.springframework.web.bind.annotation.ExceptionHandler;
  12. import org.springframework.web.bind.annotation.RestControllerAdvice;
  13. import javax.servlet.http.HttpServletRequest;
  14. import java.util.List;
  15. /**
  16. * 全局异常处理器
  17. */
  18. @RestControllerAdvice
  19. public class GlobalExceptionHandle {
  20. private static Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandle.class);
  21. @ExceptionHandler
  22. public Object handleException(HttpServletRequest req, Exception exception) throws Exception {
  23. String uri = req.getRequestURI();
  24. CommonResponse<Object> response = new CommonResponse<Object>();
  25. // 业务异常
  26. if (exception instanceof CommonException) {
  27. CommonException e = (CommonException) exception;
  28. response.setCode(e.getCode());
  29. response.setMsg(e.getMsg());
  30. LOGGER.warn("uri:" + uri + "\n" + "CustomException log.", exception);
  31. } else if (exception instanceof MethodArgumentNotValidException) {
  32. // 参数校验异常
  33. MethodArgumentNotValidException e = (MethodArgumentNotValidException) exception;
  34. List<ObjectError> errorList = e.getBindingResult().getAllErrors();
  35. StringBuilder errorMsg = new StringBuilder();
  36. errorMsg.append("|");
  37. if (!CollectionUtils.isEmpty(errorList)) {
  38. for (ObjectError objectError : errorList) {
  39. errorMsg.append(objectError.getDefaultMessage()).append("|");
  40. }
  41. }
  42. response.setCode(ExceptionEnum.PARAM_ERROR.getCode());
  43. response.setMsg(errorMsg.toString());
  44. LOGGER.warn("uri:" + uri + "\n" + "MethodArgumentNotValidException log.", exception);
  45. } else if (exception instanceof BindException) {
  46. // 参数绑定异常
  47. BindException e = (BindException) exception;
  48. List<ObjectError> errorList = e.getBindingResult().getAllErrors();
  49. StringBuilder errorMsg = new StringBuilder();
  50. errorMsg.append("|");
  51. if (!CollectionUtils.isEmpty(errorList)) {
  52. for (ObjectError objectError : errorList) {
  53. errorMsg.append(objectError.getDefaultMessage()).append("|");
  54. }
  55. }
  56. response.setCode(ExceptionEnum.PARAM_ERROR.getCode());
  57. response.setMsg(errorMsg.toString());
  58. LOGGER.warn("uri:" + uri + "\n" + "BindException log.", exception);
  59. } else {
  60. response.setCode(ExceptionEnum.SYSTEM_ERROR.getCode());
  61. response.setMsg(ExceptionEnum.SYSTEM_ERROR.getMsg());
  62. LOGGER.warn("uri:" + uri + "\n" + "unknownException log.", exception);
  63. }
  64. return response;
  65. }
  66. }