123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- package com.fdkankan.manage.exception;
- import com.fdkankan.manage.common.ResultCode;
- import com.fdkankan.manage.common.ResultData;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.dao.DataIntegrityViolationException;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.ResponseBody;
- import org.springframework.web.bind.annotation.RestControllerAdvice;
- import java.util.Objects;
- /**
- * 全局异常处理器
- */
- @RestControllerAdvice
- @Slf4j
- public class GlobalExceptionHandler {
- /**
- * 处理未知异常
- */
- @ResponseBody
- @ExceptionHandler(value = Exception.class)
- public ResultData exceptionHandler(Exception e) throws Exception {
- log.error("服务错误:", e);
- return ResultData.error( 500, e.getCause().getMessage());
- }
- /**
- * 处理业务异常
- */
- @ResponseBody
- @ExceptionHandler(value = BusinessException.class)
- public ResultData businessExceptionHandler(BusinessException e) {
- log.info("业务异常code:{},message:{}", e.getCode(), e.getMessage());
- return ResultData.error(e.getCode(), e.getMessage());
- }
- /**
- * 处理业务异常
- */
- @ResponseBody
- @ExceptionHandler(value = DataIntegrityViolationException.class)
- public ResultData DataIntegrityViolationExceptionHandler(DataIntegrityViolationException e) {
- log.error("mysql服务错误:", e);
- if(e.getCause().getMessage().contains("Data too long")){
- return ResultData.error(ResultCode.DATA_TOO_LONG);
- }
- return ResultData.error( 500, e.getCause().getMessage());
- }
- }
|