56 lines
2.2 KiB
Java
56 lines
2.2 KiB
Java
package com.guo.learningprogresstracker.common;
|
|
|
|
import cn.dev33.satoken.exception.NotLoginException;
|
|
import com.guo.learningprogresstracker.entity.CommonResult;
|
|
import com.guo.learningprogresstracker.exception.AppException;
|
|
import com.guo.learningprogresstracker.exception.ErrorParameterException;
|
|
import com.guo.learningprogresstracker.exception.NotFindEntitiesException;
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.validation.BindingResult;
|
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
|
|
|
@RestControllerAdvice
|
|
@Slf4j
|
|
public class GlobalExceptionHandler {
|
|
|
|
@ExceptionHandler({ErrorParameterException.class})
|
|
public CommonResult errorParameterException(ErrorParameterException ex) {
|
|
return CommonResult.error(ex.getMessage());
|
|
}
|
|
|
|
@ExceptionHandler({NotFindEntitiesException.class})
|
|
public CommonResult notFindEntitiesException(NotFindEntitiesException ex) {
|
|
return CommonResult.error(ex.getMessage());
|
|
}
|
|
|
|
@ExceptionHandler({AppException.class})
|
|
public CommonResult AppException(AppException ex) {
|
|
return CommonResult.serverError(ex.getMessage());
|
|
}
|
|
|
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
|
public CommonResult MyMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
|
|
BindingResult bindingResult = ex.getBindingResult();
|
|
return CommonResult.error(bindingResult.getFieldError().getDefaultMessage());
|
|
}
|
|
|
|
@ExceptionHandler(NotLoginException.class)
|
|
public CommonResult handleNotLogin(NotLoginException e, HttpServletRequest req, HttpServletResponse res) {
|
|
res.setStatus(HttpStatus.UNAUTHORIZED.value());
|
|
return new CommonResult<>(HttpStatus.UNAUTHORIZED.value(), "未登录,请重新登录", null);
|
|
}
|
|
|
|
@ExceptionHandler(Exception.class)
|
|
public CommonResult Exception(Exception ex) {
|
|
log.error("系统异常", ex);
|
|
return CommonResult.error(ex.getMessage());
|
|
}
|
|
|
|
}
|