refactor: improve error handling in manga search methods with logging

This commit is contained in:
Rodrigo Verdiani 2026-03-30 21:54:56 -03:00
parent bc8fe69e20
commit 0c056541bd

View File

@ -48,54 +48,66 @@ public class MangaResolutionService {
} }
private Optional<ProviderResult> searchMangaOnAniList(String title) { private Optional<ProviderResult> searchMangaOnAniList(String title) {
var searchResults = aniListService.searchMangaByTitle(title); try {
if (searchResults.isEmpty()) { 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(); 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<ProviderResult> searchMangaOnMyAnimeList(String title) { private Optional<ProviderResult> searchMangaOnMyAnimeList(String title) {
var searchResults = myAnimeListService.searchMangaByTitle(title); try {
if (searchResults.isEmpty()) { 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(); 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) { public Manga findOrCreateManga(Long aniListId, Long malId) {