From 28dd303e547254134f8f61e5720608ba0cc56e90 Mon Sep 17 00:00:00 2001 From: James Ward Date: Sat, 11 Apr 2026 15:50:33 -0400 Subject: [PATCH] fix(kobo): handle missing book when pulling kobo metadata (#473) instead of an uncaught error, missing books should be treated as a 404 not-found in the kobo metadata endpoint --- .../booklore/controller/KoboController.java | 10 ++++++--- .../service/kobo/KoboEntitlementService.java | 9 +++++--- .../service/KoboEntitlementServiceTest.java | 21 +++++++++++++++++++ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/booklore-api/src/main/java/org/booklore/controller/KoboController.java b/booklore-api/src/main/java/org/booklore/controller/KoboController.java index f5cda9f14..dbd4703fb 100644 --- a/booklore-api/src/main/java/org/booklore/controller/KoboController.java +++ b/booklore-api/src/main/java/org/booklore/controller/KoboController.java @@ -124,12 +124,16 @@ public class KoboController { @GetMapping("/v1/library/{bookId}/metadata") public ResponseEntity getBookMetadata(@Parameter(description = "Book ID") @PathVariable String bookId) { if (StringUtils.isNumeric(bookId)) { - return ResponseEntity.ok(List.of(koboEntitlementService.getMetadataForBook(Long.parseLong(bookId), token))); + KoboBookMetadata metadata = koboEntitlementService.getMetadataForBook(Long.parseLong(bookId), token); + + if (metadata != null) { + return ResponseEntity.ok(List.of(metadata)); + } } else if(isForwardingToKoboStore()) { return koboServerProxy.proxyCurrentRequest(null, false); - } else { - throw ApiError.GENERIC_NOT_FOUND.createException("Not Found"); } + + throw ApiError.GENERIC_NOT_FOUND.createException("Not Found"); } @Operation(summary = "Get reading state", description = "Retrieve the reading state for a book.") diff --git a/booklore-api/src/main/java/org/booklore/service/kobo/KoboEntitlementService.java b/booklore-api/src/main/java/org/booklore/service/kobo/KoboEntitlementService.java index a3c53bf65..c8b5e21f0 100644 --- a/booklore-api/src/main/java/org/booklore/service/kobo/KoboEntitlementService.java +++ b/booklore-api/src/main/java/org/booklore/service/kobo/KoboEntitlementService.java @@ -276,11 +276,14 @@ public class KoboEntitlementService { } public KoboBookMetadata getMetadataForBook(long bookId, String token) { - List books = bookQueryService.findAllWithMetadataByIds(Set.of(bookId)) + Optional book = bookQueryService.findAllWithMetadataByIds(Set.of(bookId)) .stream() .filter(koboCompatibilityService::isBookSupportedForKobo) - .toList(); - return mapToKoboMetadata(books.getFirst(), token); + .findFirst(); + + return book + .map(bookEntity -> mapToKoboMetadata(bookEntity, token)) + .orElse(null); } private KoboBookMetadata mapToKoboMetadata(BookEntity book, String token) { diff --git a/booklore-api/src/test/java/org/booklore/service/KoboEntitlementServiceTest.java b/booklore-api/src/test/java/org/booklore/service/KoboEntitlementServiceTest.java index d94820906..e745ea98d 100644 --- a/booklore-api/src/test/java/org/booklore/service/KoboEntitlementServiceTest.java +++ b/booklore-api/src/test/java/org/booklore/service/KoboEntitlementServiceTest.java @@ -52,6 +52,7 @@ import java.time.Instant; import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.ZoneOffset; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Optional; @@ -137,6 +138,26 @@ class KoboEntitlementServiceTest { verify(koboCompatibilityService).isBookSupportedForKobo(cbxBook); } + @Test + void getMetadataForBook_shouldReturnNullWhenNoBook() { + long bookId = 1L; + String token = "test-token"; + + BookEntity cbxBook = createCbxBookEntity(bookId); + when(bookQueryService.findAllWithMetadataByIds(Set.of(bookId))) + .thenReturn(Collections.emptyList()); + when(koboCompatibilityService.isBookSupportedForKobo(cbxBook)) + .thenReturn(true); + when(koboUrlBuilder.downloadUrl(token, bookId)) + .thenReturn("http://test.com/download/" + bookId); + when(appSettingService.getAppSettings()) + .thenReturn(createAppSettingsWithKoboSettings()); + + KoboBookMetadata result = koboEntitlementService.getMetadataForBook(bookId, token); + + assertNull(result); + } + @Test void mapToKoboMetadata_cbxBookWithConversionEnabled_shouldReturnEpubFormat() { long bookId = 1L;