GlobalExceptionHandler.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package space.anyi.petServiceManagementSystem.server.config;
  2. import org.springframework.dao.DuplicateKeyException;
  3. import org.springframework.http.HttpStatus;
  4. import org.springframework.validation.BindException;
  5. import org.springframework.web.bind.MethodArgumentNotValidException;
  6. import org.springframework.web.bind.MissingServletRequestParameterException;
  7. import org.springframework.web.bind.annotation.ExceptionHandler;
  8. import org.springframework.web.bind.annotation.ResponseStatus;
  9. import org.springframework.web.bind.annotation.RestControllerAdvice;
  10. import space.anyi.petServiceManagementSystem.server.common.BusinessException;
  11. import space.anyi.petServiceManagementSystem.server.common.ForbiddenException;
  12. import space.anyi.petServiceManagementSystem.server.common.R;
  13. import space.anyi.petServiceManagementSystem.server.common.ResourceNotFoundException;
  14. import space.anyi.petServiceManagementSystem.server.common.UnauthorizedException;
  15. import java.util.Objects;
  16. @RestControllerAdvice
  17. public class GlobalExceptionHandler {
  18. @ExceptionHandler(BusinessException.class)
  19. @ResponseStatus(HttpStatus.BAD_REQUEST)
  20. public R<Void> handleBusinessException(BusinessException e) {
  21. return R.error(e.getCode(), e.getMessage());
  22. }
  23. @ExceptionHandler(UnauthorizedException.class)
  24. @ResponseStatus(HttpStatus.UNAUTHORIZED)
  25. public R<Void> handleUnauthorized(UnauthorizedException e) {
  26. return R.error(e.getCode(), e.getMessage());
  27. }
  28. @ExceptionHandler(ForbiddenException.class)
  29. @ResponseStatus(HttpStatus.FORBIDDEN)
  30. public R<Void> handleForbidden(ForbiddenException e) {
  31. return R.error(e.getCode(), e.getMessage());
  32. }
  33. @ExceptionHandler(ResourceNotFoundException.class)
  34. @ResponseStatus(HttpStatus.NOT_FOUND)
  35. public R<Void> handleNotFound(ResourceNotFoundException e) {
  36. return R.error(e.getCode(), e.getMessage());
  37. }
  38. @ExceptionHandler(MethodArgumentNotValidException.class)
  39. @ResponseStatus(HttpStatus.BAD_REQUEST)
  40. public R<Void> handleValidation(MethodArgumentNotValidException e) {
  41. String msg = Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage();
  42. return R.error(400, msg);
  43. }
  44. @ExceptionHandler(BindException.class)
  45. @ResponseStatus(HttpStatus.BAD_REQUEST)
  46. public R<Void> handleBindException(BindException e) {
  47. String msg = Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage();
  48. return R.error(400, msg);
  49. }
  50. @ExceptionHandler(MissingServletRequestParameterException.class)
  51. @ResponseStatus(HttpStatus.BAD_REQUEST)
  52. public R<Void> handleMissingParam(MissingServletRequestParameterException e) {
  53. return R.error(400, "缺少必填参数: " + e.getParameterName());
  54. }
  55. @ExceptionHandler(DuplicateKeyException.class)
  56. @ResponseStatus(HttpStatus.CONFLICT)
  57. public R<Void> handleDuplicateKey(DuplicateKeyException e) {
  58. return R.error(409, "数据重复,请检查唯一字段");
  59. }
  60. @ExceptionHandler(Exception.class)
  61. @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  62. public R<Void> handleException(Exception e) {
  63. e.printStackTrace();
  64. return R.error(500, "服务器内部错误");
  65. }
  66. }