fix(api): use ApiException instead of EntityNotFoundExcepion (#2302)

This commit is contained in:
James Ward
2026-08-09 23:03:51 -04:00
committed by GitHub
parent 97226de24e
commit e88de1a495
11 changed files with 43 additions and 44 deletions
@@ -11,6 +11,7 @@ public enum ApiError {
GENERIC_BAD_REQUEST(HttpStatus.BAD_REQUEST, "%s"),
GENERIC_UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "%s"),
RESOURCE_NOT_FOUND(HttpStatus.NOT_FOUND, "%s not found with ID: %d"),
BOOK_NOT_FOUND(HttpStatus.NOT_FOUND, "Book not found with ID: %d"),
EMAIL_PROVIDER_NOT_FOUND(HttpStatus.NOT_FOUND, "Email provider with ID %s not found"),
DEFAULT_EMAIL_PROVIDER_NOT_FOUND(HttpStatus.NOT_FOUND, "Default email provider not found"),
@@ -2,6 +2,7 @@ package org.booklore.service.book;
import org.booklore.config.security.service.AuthenticationService;
import org.booklore.exception.APIException;
import org.booklore.exception.ApiError;
import org.booklore.mapper.AnnotationMapper;
import org.booklore.model.dto.Annotation;
import org.booklore.model.dto.CreateAnnotationRequest;
@@ -12,7 +13,6 @@ import org.booklore.model.entity.BookLoreUserEntity;
import org.booklore.repository.AnnotationRepository;
import org.booklore.repository.BookRepository;
import org.booklore.repository.UserRepository;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
@@ -94,17 +94,17 @@ public class AnnotationService {
private AnnotationEntity findAnnotationByIdAndUser(Long annotationId) {
Long userId = getCurrentUserId();
return annotationRepository.findByIdAndUserId(annotationId, userId)
.orElseThrow(() -> new EntityNotFoundException("Annotation not found: " + annotationId));
.orElseThrow(() -> ApiError.RESOURCE_NOT_FOUND.createException("Annotation", annotationId));
}
private BookEntity findBook(Long bookId) {
return bookRepository.findById(bookId)
.orElseThrow(() -> new EntityNotFoundException("Book not found: " + bookId));
.orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
}
private BookLoreUserEntity findUser(Long userId) {
return userRepository.findById(userId)
.orElseThrow(() -> new EntityNotFoundException("User not found: " + userId));
.orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(userId));
}
private void validateNoDuplicateAnnotation(String cfi, Long bookId, Long userId) {
@@ -1,6 +1,7 @@
package org.booklore.service.book;
import org.booklore.config.BookmarkProperties;
import org.booklore.exception.ApiError;
import org.booklore.mapper.BookMarkMapper;
import org.booklore.model.dto.BookMark;
import org.booklore.model.dto.CreateBookMarkRequest;
@@ -13,7 +14,6 @@ import org.booklore.repository.BookRepository;
import org.booklore.repository.UserRepository;
import org.booklore.config.security.service.AuthenticationService;
import org.booklore.exception.APIException;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
@@ -111,17 +111,17 @@ public class BookMarkService {
private BookMarkEntity findBookmarkByIdAndUser(Long bookmarkId) {
Long userId = getCurrentUserId();
return bookMarkRepository.findByIdAndUserId(bookmarkId, userId)
.orElseThrow(() -> new EntityNotFoundException("Bookmark not found: " + bookmarkId));
.orElseThrow(() -> ApiError.RESOURCE_NOT_FOUND.createException("Bookmark", bookmarkId));
}
private BookEntity findBook(Long bookId) {
return bookRepository.findById(bookId)
.orElseThrow(() -> new EntityNotFoundException("Book not found: " + bookId));
.orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
}
private BookLoreUserEntity findUser(Long userId) {
return userRepository.findById(userId)
.orElseThrow(() -> new EntityNotFoundException("User not found: " + userId));
.orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(userId));
}
private void validateNoDuplicateBookmark(String cfi, Long bookId, Long userId) {
@@ -12,7 +12,6 @@ import org.booklore.model.entity.BookNoteEntity;
import org.booklore.repository.BookNoteRepository;
import org.booklore.repository.BookRepository;
import org.booklore.repository.UserRepository;
import jakarta.persistence.EntityNotFoundException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -46,13 +45,13 @@ public class BookNoteService {
.orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(request.getBookId()));
BookLoreUserEntity user = userRepository.findById(currentUser.getId())
.orElseThrow(() -> new EntityNotFoundException("User not found: " + currentUser.getId()));
.orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(currentUser.getId()));
BookNoteEntity noteEntity;
if (request.getId() != null) {
noteEntity = bookNoteRepository.findByIdAndUserId(request.getId(), currentUser.getId())
.orElseThrow(() -> new EntityNotFoundException("Note not found: " + request.getId()));
.orElseThrow(() -> ApiError.RESOURCE_NOT_FOUND.createException("Note", request.getId()));
noteEntity.setTitle(request.getTitle());
noteEntity.setContent(request.getContent());
} else {
@@ -71,7 +70,8 @@ public class BookNoteService {
@Transactional
public void deleteNote(Long noteId) {
BookLoreUser currentUser = authenticationService.getAuthenticatedUser();
BookNoteEntity note = bookNoteRepository.findByIdAndUserId(noteId, currentUser.getId()).orElseThrow(() -> new EntityNotFoundException("Note not found: " + noteId));
BookNoteEntity note = bookNoteRepository.findByIdAndUserId(noteId, currentUser.getId())
.orElseThrow(() -> ApiError.RESOURCE_NOT_FOUND.createException("Note", noteId));
bookNoteRepository.delete(note);
}
}
@@ -2,6 +2,7 @@ package org.booklore.service.book;
import org.booklore.config.security.service.AuthenticationService;
import org.booklore.exception.APIException;
import org.booklore.exception.ApiError;
import org.booklore.mapper.BookNoteV2Mapper;
import org.booklore.model.dto.BookNoteV2;
import org.booklore.model.dto.CreateBookNoteV2Request;
@@ -12,7 +13,6 @@ import org.booklore.model.entity.BookNoteV2Entity;
import org.booklore.repository.BookNoteV2Repository;
import org.booklore.repository.BookRepository;
import org.booklore.repository.UserRepository;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
@@ -92,17 +92,17 @@ public class BookNoteV2Service {
private BookNoteV2Entity findNoteByIdAndUser(Long noteId) {
Long userId = getCurrentUserId();
return bookNoteV2Repository.findByIdAndUserId(noteId, userId)
.orElseThrow(() -> new EntityNotFoundException("Note not found: " + noteId));
.orElseThrow(() -> ApiError.RESOURCE_NOT_FOUND.createException("Note", noteId));
}
private BookEntity findBook(Long bookId) {
return bookRepository.findById(bookId)
.orElseThrow(() -> new EntityNotFoundException("Book not found: " + bookId));
.orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
}
private BookLoreUserEntity findUser(Long userId) {
return userRepository.findById(userId)
.orElseThrow(() -> new EntityNotFoundException("User not found: " + userId));
.orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(userId));
}
private void validateNoDuplicateNote(String cfi, Long bookId, Long userId) {
@@ -1,9 +1,9 @@
package org.booklore.service.book;
import jakarta.persistence.EntityNotFoundException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.booklore.config.security.service.AuthenticationService;
import org.booklore.exception.APIException;
import org.booklore.exception.ApiError;
import org.booklore.mapper.BookReviewMapper;
import org.booklore.model.dto.BookLoreUser;
@@ -98,7 +98,7 @@ public class BookReviewService {
@Transactional
public void delete(Long id) {
if (!bookReviewRepository.existsById(id)) {
throw new EntityNotFoundException("Review not found: " + id);
throw ApiError.RESOURCE_NOT_FOUND.createException("Review", id);
}
bookReviewRepository.deleteById(id);
}
@@ -2,13 +2,13 @@ package org.booklore.service.book;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.booklore.exception.ApiError;
import org.booklore.model.entity.BookEntity;
import org.booklore.model.entity.BookLoreUserEntity;
import org.booklore.model.entity.PdfAnnotationEntity;
import org.booklore.repository.BookRepository;
import org.booklore.repository.PdfAnnotationRepository;
import org.booklore.repository.UserRepository;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@@ -53,11 +53,11 @@ public class PdfAnnotationPersistenceHandler {
private BookEntity findBook(Long bookId) {
return bookRepository.findById(bookId)
.orElseThrow(() -> new EntityNotFoundException("Book not found: " + bookId));
.orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
}
private BookLoreUserEntity findUser(Long userId) {
return userRepository.findById(userId)
.orElseThrow(() -> new EntityNotFoundException("User not found: " + userId));
.orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(userId));
}
}