1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.access.AccessDeniedException; import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.validation.FieldError; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice public class GlobalExceptionHandler {
@Value("${spring.profiles.active}") private String activeProfile;
@ExceptionHandler(CustomException.class) public Object customException(CustomException e) { if ("dev".equals(activeProfile)) { e.printStackTrace(); } return Result.fail(e.getMessage(), e.getErrCode().errCode(), e.getStatus()); }
@ExceptionHandler(CustomRuntimeException.class) public ResponseEntity<?> customRuntimeException(CustomRuntimeException e) { if ("dev".equals(activeProfile)) { e.printStackTrace(); } return Result.fail(e.getMessage(), e.getErrCode().errCode(), e.getStatus()); }
@ExceptionHandler(Exception.class) public ResponseEntity<?> exception(Exception e) { if ("dev".equals(activeProfile)) { e.printStackTrace(); } return Result.fail(e.getMessage(), ErrCodeEnum.UNTREATED_FRAMEWORK_EX.errCode(), HttpStatus.INTERNAL_SERVER_ERROR); }
@ExceptionHandler(RuntimeException.class) public ResponseEntity<?> runtimeException(RuntimeException e) { if ("dev".equals(activeProfile)) { e.printStackTrace(); } return Result.fail(e.getMessage(), ErrCodeEnum.UNTREATED_FRAMEWORK_RE.errCode(), HttpStatus.INTERNAL_SERVER_ERROR); }
@ExceptionHandler(AccessDeniedException.class) public ResponseEntity<?> accessDeniedException(AccessDeniedException e) { if ("dev".equals(activeProfile)) { e.printStackTrace(); } return Result.fail(e.getMessage(), {ErrCodeEnum.errCode()}, HttpStatus.FORBIDDEN); }
@ExceptionHandler({BadCredentialsException.class, UsernameNotFoundException.class}) public ResponseEntity<?> usernameNotFoundException(Exception e) { if ("dev".equals(activeProfile)) { e.printStackTrace(); } return Result.fail("用户名或密码错误", {ErrCodeEnum.errCode()}, HttpStatus.BAD_REQUEST); }
@ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity<?> methodArgumentNotValidException(MethodArgumentNotValidException e) { if ("dev".equals(activeProfile)) { e.printStackTrace(); } FieldError fieldError = exception .getBindingResult() .getFieldErrors() .iterator() .next(); return Result.fail(fieldError.getDefaultMessage(), {ErrCodeEnum.errCode()}, HttpStatus.BAD_REQUEST); }
}
|