From 0c056541bdb5e2d2d5979d265caa7252767edbce Mon Sep 17 00:00:00 2001 From: Rodrigo Verdiani Date: Mon, 30 Mar 2026 21:54:56 -0300 Subject: [PATCH] refactor: improve error handling in manga search methods with logging --- .../service/MangaResolutionService.java | 94 +++++++++++-------- 1 file changed, 53 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..dfc48d5 100644 --- a/src/main/java/com/magamochi/catalog/service/MangaResolutionService.java +++ b/src/main/java/com/magamochi/catalog/service/MangaResolutionService.java @@ -48,54 +48,66 @@ 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 for manga with title {} on AniList: {}", title, e.getMessage()); 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 for manga with title {} on MyAnimeList: {}", title, e.getMessage()); 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) {