1. @Data
    2. public class ErrorResult implements Serializable {
    3. private Integer code;
    4. private String message;
    5. private boolean success = false;
    6. @JsonIgnore
    7. private ResultCode resultCode;
    8. public static ErrorResult error() {
    9. ErrorResult result = new ErrorResult();
    10. result.setResultCode(ResultCode.INTERNAL_ERROR);
    11. return result;
    12. }
    13. public static ErrorResult error(String message) {
    14. ErrorResult result = new ErrorResult();
    15. result.setCode(ResultCode.INTERNAL_ERROR.code());
    16. result.setMessage(message);
    17. return result;
    18. }
    19. public static ErrorResult error(Integer code, String message) {
    20. ErrorResult result = new ErrorResult();
    21. result.setCode(code);
    22. result.setMessage(message);
    23. return result;
    24. }
    25. public static ErrorResult error(ResultCode resultCode, String message) {
    26. ErrorResult result = new ErrorResult();
    27. result.setResultCode(resultCode);
    28. result.setMessage(message)
    29. return result;
    30. }
    31. }
    1. @Data
    2. public class BaseException extends RuntimeException {
    3. private static final int BASE_EXCEPTION_CODE = ResultCode.INTERNAL_ERROR.code();
    4. private static final String BASE_EXCEPTION_MESSAGE = ResultCode.INTERNAL_ERROR.message();
    5. private Integer code;
    6. private String message;
    7. public BaseException() {
    8. super(BASE_EXCEPTION_MESSAGE);
    9. this.code = BASE_EXCEPTION_CODE;
    10. this.message = BASE_EXCEPTION_MESSAGE;
    11. }
    12. public BaseException(String message) {
    13. super(message);
    14. this.code = BASE_EXCEPTION_CODE;
    15. this.message = message;
    16. }
    17. public BaseException(ResultCode resultCode) {
    18. super(resultCode.message());
    19. this.code = resultCode.code();
    20. this.message = resultCode.message();
    21. }
    22. public BaseException(Throwable cause) {
    23. super(cause);
    24. this.code = BASE_EXCEPTION_CODE;
    25. this.message = BASE_EXCEPTION_MESSAGE;
    26. }
    27. public BaseException(String message, Throwable cause) {
    28. super(message, cause);
    29. this.code = BASE_EXCEPTION_CODE;
    30. this.message = message;
    31. }
    32. public BaseException(Integer code, String message) {
    33. super(message);
    34. this.code = code;
    35. this.message = message;
    36. }
    37. public BaseException(Integer code, String message, Throwable cause) {
    38. super(message, cause);
    39. this.code = code;
    40. this.message = message;
    41. }
    42. }
    1. public class BizException extends BaseException {
    2. public BizException(ResultCode resultCode) {
    3. super(resultCode);
    4. }
    5. }
    1. @Slf4j
    2. @RestControllerAdvice
    3. public class GlobalExceptionHandler {
    4. /**
    5. * 统一处理自定义基础异常
    6. */
    7. @ExceptionHandler(BaseException.class)
    8. public ErrorResult baseException(BaseException e) {
    9. if (StringUtils.isEmpty(e.getCode())) {
    10. return ErrorResult.error(e.getMessage());
    11. }
    12. return ErrorResult.error(e.getCode(), e.getMessage());
    13. }
    14. /**
    15. * 统一处理自定义业务异常
    16. */
    17. @ExceptionHandler(BizException.class)
    18. public ErrorResult bizException(BizException e) {
    19. if (StringUtils.isEmpty(e.getCode())) {
    20. return ErrorResult.error(e.getMessage());
    21. }
    22. return ErrorResult.error(e.getCode(), e.getMessage());
    23. }
    24. /**
    25. * 统一处理非自定义异常外的所有异常
    26. */
    27. @ExceptionHandler(Exception.class)
    28. public ErrorResult handleException(Exception e) {
    29. log.error(e.getMessage(), e);
    30. return ErrorResult.error(e.getMessage());
    31. }
    32. /**
    33. * 兼容Validation校验框架:忽略参数异常处理器
    34. */
    35. @ExceptionHandler(MissingServletRequestParameterException.class)
    36. public ApiResult<String> parameterMissingExceptionHandler(MissingServletRequestParameterException e) {
    37. log.error(e.getMessage(), e);
    38. return ErrorResult.error(PARAMETER_EXCEPTION, "请求参数 " + e.getParameterName() + " 不能为空");
    39. }
    40. /**
    41. * 兼容Validation校验框架:缺少请求体异常处理器
    42. */
    43. @ExceptionHandler(HttpMessageNotReadableException.class)
    44. public ErrorResult parameterBodyMissingExceptionHandler(HttpMessageNotReadableException e) {
    45. log.error(e.getMessage(), e);
    46. return ErrorResult.error(PARAMETER_EXCEPTION, "参数体不能为空");
    47. }
    48. /**
    49. * 兼容Validation校验框架:参数效验异常处理器
    50. */
    51. @ExceptionHandler(MethodArgumentNotValidException.class)
    52. public ErrorResult parameterExceptionHandler(MethodArgumentNotValidException e) {
    53. log.error(e.getMessage(), e);
    54. // 获取异常信息
    55. BindingResult exceptions = e.getBindingResult();
    56. // 判断异常中是否有错误信息,如果存在就使用异常中的消息,否则使用默认消息
    57. if (exceptions.hasErrors()) {
    58. List<ObjectError> errors = exceptions.getAllErrors();
    59. if (!errors.isEmpty()) {
    60. // 这里列出了全部错误参数,按正常逻辑,只需要第一条错误即可
    61. FieldError fieldError = (FieldError) errors.get(0);
    62. return ErrorResult.error(PARAMETER_EXCEPTION, fieldError.getDefaultMessage());
    63. }
    64. }
    65. return ErrorResult.error(PARAMETER_EXCEPTION, "请求参数校验异常");
    66. }
    67. }