12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- package com.fdkankan.manage_jp.exception;
- import com.fdkankan.manage_jp.common.Result;
- import com.fdkankan.manage_jp.common.ResultCode;
- 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;
- /**
- * 全局异常处理器
- */
- @RestControllerAdvice
- @Slf4j
- public class GlobalExceptionHandler {
- /**
- * 处理未知异常
- */
- @ResponseBody
- @ExceptionHandler(value = Exception.class)
- public Result exceptionHandler(Exception e) throws Exception {
- log.error("服务错误:", e);
- return Result.failure( 500, e.getCause().getMessage());
- }
- /**
- * 处理业务异常
- */
- @ResponseBody
- @ExceptionHandler(value = BusinessException.class)
- public Result businessExceptionHandler(BusinessException e) {
- log.info("业务异常code:{},message:{}", e.getCode(), e.getMessage());
- return Result.failure(e.getCode(), e.getMessage());
- }
- /**
- * 处理业务异常
- */
- @ResponseBody
- @ExceptionHandler(value = DataIntegrityViolationException.class)
- public Result DataIntegrityViolationExceptionHandler(DataIntegrityViolationException e) {
- log.error("mysql服务错误:", e);
- if(e.getCause().getMessage().contains("Data too long")){
- return Result.failure(ResultCode.DATA_TOO_LONG);
- }
- return Result.failure( 500, e.getCause().getMessage());
- }
- }
|