| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- package space.anyi.petServiceManagementSystem.server.config;
- import org.springframework.dao.DuplicateKeyException;
- import org.springframework.http.HttpStatus;
- import org.springframework.validation.BindException;
- import org.springframework.web.bind.MethodArgumentNotValidException;
- import org.springframework.web.bind.MissingServletRequestParameterException;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.ResponseStatus;
- import org.springframework.web.bind.annotation.RestControllerAdvice;
- import space.anyi.petServiceManagementSystem.server.common.BusinessException;
- import space.anyi.petServiceManagementSystem.server.common.ForbiddenException;
- import space.anyi.petServiceManagementSystem.server.common.R;
- import space.anyi.petServiceManagementSystem.server.common.ResourceNotFoundException;
- import space.anyi.petServiceManagementSystem.server.common.UnauthorizedException;
- import java.util.Objects;
- @RestControllerAdvice
- public class GlobalExceptionHandler {
- @ExceptionHandler(BusinessException.class)
- @ResponseStatus(HttpStatus.BAD_REQUEST)
- public R<Void> handleBusinessException(BusinessException e) {
- return R.error(e.getCode(), e.getMessage());
- }
- @ExceptionHandler(UnauthorizedException.class)
- @ResponseStatus(HttpStatus.UNAUTHORIZED)
- public R<Void> handleUnauthorized(UnauthorizedException e) {
- return R.error(e.getCode(), e.getMessage());
- }
- @ExceptionHandler(ForbiddenException.class)
- @ResponseStatus(HttpStatus.FORBIDDEN)
- public R<Void> handleForbidden(ForbiddenException e) {
- return R.error(e.getCode(), e.getMessage());
- }
- @ExceptionHandler(ResourceNotFoundException.class)
- @ResponseStatus(HttpStatus.NOT_FOUND)
- public R<Void> handleNotFound(ResourceNotFoundException e) {
- return R.error(e.getCode(), e.getMessage());
- }
- @ExceptionHandler(MethodArgumentNotValidException.class)
- @ResponseStatus(HttpStatus.BAD_REQUEST)
- public R<Void> handleValidation(MethodArgumentNotValidException e) {
- String msg = Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage();
- return R.error(400, msg);
- }
- @ExceptionHandler(BindException.class)
- @ResponseStatus(HttpStatus.BAD_REQUEST)
- public R<Void> handleBindException(BindException e) {
- String msg = Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage();
- return R.error(400, msg);
- }
- @ExceptionHandler(MissingServletRequestParameterException.class)
- @ResponseStatus(HttpStatus.BAD_REQUEST)
- public R<Void> handleMissingParam(MissingServletRequestParameterException e) {
- return R.error(400, "缺少必填参数: " + e.getParameterName());
- }
- @ExceptionHandler(DuplicateKeyException.class)
- @ResponseStatus(HttpStatus.CONFLICT)
- public R<Void> handleDuplicateKey(DuplicateKeyException e) {
- return R.error(409, "数据重复,请检查唯一字段");
- }
- @ExceptionHandler(Exception.class)
- @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
- public R<Void> handleException(Exception e) {
- e.printStackTrace();
- return R.error(500, "服务器内部错误");
- }
- }
|