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
This commit is contained in:
James Ward
2026-04-22 17:27:13 -04:00
committed by Zach Yale
parent d4bc8407d8
commit 28dd303e54
3 changed files with 34 additions and 6 deletions
@@ -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.")
@@ -276,11 +276,14 @@ public class KoboEntitlementService {
}
public KoboBookMetadata getMetadataForBook(long bookId, String token) {
List<BookEntity> books = bookQueryService.findAllWithMetadataByIds(Set.of(bookId))
Optional<BookEntity> 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) {
@@ -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;