RestExceptionHandler.java 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package com.szwl.exception;
  2. import com.alibaba.fastjson.JSON;
  3. import com.szwl.constant.ResponseCodesEnum;
  4. import com.szwl.model.bo.R;
  5. import com.szwl.model.bo.ResponseModel;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.springframework.validation.BindException;
  8. import org.springframework.validation.BindingResult;
  9. import org.springframework.validation.FieldError;
  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. @Slf4j
  15. @RestControllerAdvice
  16. public class RestExceptionHandler {
  17. @ExceptionHandler(Exception.class)
  18. public ResponseModel<Void> otherExceptionHandler(Exception exception) {
  19. log.error("un caught exception: ", exception);
  20. return R.fail("内部异常[%s],请联系管理员。", exception.getMessage());
  21. }
  22. @ExceptionHandler(MyException.class)
  23. public ResponseModel<Void> myExceptionHandler(Exception exception) {
  24. log.error("my exception: ", exception);
  25. return R.fail("业务异常[%s],请联系管理员。", exception.getMessage());
  26. }
  27. @ExceptionHandler(ParamNotFoundException.class)
  28. public ResponseModel<Void> paramNotFoundExceptionHandler(Exception exception) {
  29. log.error("param not found exception: ", exception);
  30. return R.fail(ResponseCodesEnum.A0001,"参数异常[%s],请联系管理员。", exception.getMessage());
  31. }
  32. /**
  33. * 处理业务异常
  34. *
  35. * @param e
  36. * @return
  37. */
  38. @ExceptionHandler(BizException.class)
  39. public ResponseModel<Void> handlerBizException(BizException e) {
  40. log.error("biz exception: {}", e.getMessage());
  41. return R.fail(e.getResponseCodesEnum(), "业务异常[%s],请联系管理员。", e.getMessage());
  42. }
  43. /**
  44. * 校验错误拦截处理
  45. *
  46. * @param ex 错误信息集合
  47. * @return 错误信息
  48. * @Title: methodArgumentNotValidHandler
  49. */
  50. @ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
  51. public ResponseModel<Void> methodArgumentNotValidHandler(HttpServletRequest request, Exception ex) {
  52. BindingResult result = null;
  53. if (ex instanceof BindException) {
  54. BindException bindException = (BindException) ex;
  55. result = bindException.getBindingResult();
  56. } else if (ex instanceof MethodArgumentNotValidException) {
  57. MethodArgumentNotValidException methodArgumentNotValidException = (MethodArgumentNotValidException) ex;
  58. result = methodArgumentNotValidException.getBindingResult();
  59. }
  60. StringBuilder msg = new StringBuilder();
  61. if (result != null) {
  62. if (result.getFieldErrorCount() > 0) {
  63. for (FieldError fieldErr : result.getFieldErrors()) {
  64. msg.append(fieldErr.getField() + ":" + fieldErr.getDefaultMessage()).append("|");
  65. }
  66. }
  67. log.error("error by url:{}", request.getRequestURI().toString());
  68. log.error("request param:{}", JSON.toJSONString(result.getTarget()));
  69. log.error("response msg:{}", msg.toString());
  70. }
  71. return R.fail(ResponseCodesEnum.A0001, "接口参数错误:%s", msg);
  72. }
  73. /**
  74. * 认证异常
  75. *
  76. * @param exception
  77. * @return
  78. */
  79. @ExceptionHandler(AuthenticationException.class)
  80. public ResponseModel<Void> authenticationExceptionHandler(AuthenticationException exception) {
  81. log.error("common authentication exception: ", exception);
  82. return R.fail(exception.getResponseCodesEnum(), exception.getMessage());
  83. }
  84. }