From e6c96c55296e2f7ff4642f7d15bf39d8d5918797 Mon Sep 17 00:00:00 2001 From: Rodrigo Verdiani Date: Mon, 30 Mar 2026 23:11:06 -0300 Subject: [PATCH] refactor: improve error handling in manga search methods with logging --- .../service/MangaResolutionService.java | 92 ++++++++++--------- 1 file changed, 51 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/magamochi/catalog/service/MangaResolutionService.java b/src/main/java/com/magamochi/catalog/service/MangaResolutionService.java index c27ad1b..1f6332c 100644 --- a/src/main/java/com/magamochi/catalog/service/MangaResolutionService.java +++ b/src/main/java/com/magamochi/catalog/service/MangaResolutionService.java @@ -48,54 +48,64 @@ public class MangaResolutionService { } private Optional searchMangaOnAniList(String title) { - var searchResults = aniListService.searchMangaByTitle(title); - if (searchResults.isEmpty()) { + try { + var searchResults = aniListService.searchMangaByTitle(title); + if (searchResults.isEmpty()) { + return Optional.empty(); + } + + var matchResponse = + titleMatcherService.findBestMatch( + TitleMatcherService.TitleMatchRequest.builder() + .title(title) + .options(searchResults.keySet()) + .build()); + + if (!matchResponse.matchFound()) { + log.warn("No title match found for manga with title {} on AniList", title); + return Optional.empty(); + } + + var matchedManga = searchResults.get(matchResponse.bestMatch()); + + var bestTitle = + nonNull(matchedManga.title().romaji()) + ? matchedManga.title().romaji() + : matchedManga.title().english(); + + return Optional.of(new ProviderResult(bestTitle, matchedManga.id())); + } catch (Exception e) { + log.error("Error searching manga with title {} on AniList", title, e); return Optional.empty(); } - - var matchResponse = - titleMatcherService.findBestMatch( - TitleMatcherService.TitleMatchRequest.builder() - .title(title) - .options(searchResults.keySet()) - .build()); - - if (!matchResponse.matchFound()) { - log.warn("No title match found for manga with title {} on AniList", title); - return Optional.empty(); - } - - var matchedManga = searchResults.get(matchResponse.bestMatch()); - - var bestTitle = - nonNull(matchedManga.title().romaji()) - ? matchedManga.title().romaji() - : matchedManga.title().english(); - - return Optional.of(new ProviderResult(bestTitle, matchedManga.id())); } private Optional searchMangaOnMyAnimeList(String title) { - var searchResults = myAnimeListService.searchMangaByTitle(title); - if (searchResults.isEmpty()) { + try { + var searchResults = myAnimeListService.searchMangaByTitle(title); + if (searchResults.isEmpty()) { + return Optional.empty(); + } + + var matchResponse = + titleMatcherService.findBestMatch( + TitleMatcherService.TitleMatchRequest.builder() + .title(title) + .options(searchResults.keySet()) + .build()); + if (!matchResponse.matchFound()) { + log.warn("No title match found for manga with title {} on MyAnimeList", title); + return Optional.empty(); + } + + var bestTitle = matchResponse.bestMatch(); + var malId = searchResults.get(bestTitle); + + return Optional.of(new ProviderResult(bestTitle, malId)); + } catch (Exception e) { + log.error("Error searching manga with title {} on MyAnimeList", title, e); return Optional.empty(); } - - var matchResponse = - titleMatcherService.findBestMatch( - TitleMatcherService.TitleMatchRequest.builder() - .title(title) - .options(searchResults.keySet()) - .build()); - if (!matchResponse.matchFound()) { - log.warn("No title match found for manga with title {} on MyAnimeList", title); - return Optional.empty(); - } - - var bestTitle = matchResponse.bestMatch(); - var malId = searchResults.get(bestTitle); - - return Optional.of(new ProviderResult(bestTitle, malId)); } public Manga findOrCreateManga(Long aniListId, Long malId) {