Fix LRCLib and SquidWTF error handling
Some checks failed
CI / build-and-test (push) Has been cancelled

- Handle nullable duration in LRCLib API responses
- Validate input parameters before making LRCLib requests
- Change SquidWTF artist warning to debug level (expected behavior)
- Prevent JSON deserialization errors when duration is null
- Prevent 400 Bad Request errors from empty track names
This commit is contained in:
2026-02-06 02:07:45 -05:00
parent 8a3abdcbf7
commit ad5fea7d8e
2 changed files with 15 additions and 7 deletions

View File

@@ -30,6 +30,14 @@ public class LrclibService
public async Task<LyricsInfo?> GetLyricsAsync(string trackName, string[] artistNames, string albumName, int durationSeconds)
{
// Validate input parameters
if (string.IsNullOrWhiteSpace(trackName) || artistNames == null || artistNames.Length == 0)
{
_logger.LogDebug("Invalid parameters for lyrics search: trackName={TrackName}, artistCount={ArtistCount}",
trackName, artistNames?.Length ?? 0);
return null;
}
var artistName = string.Join(", ", artistNames);
var cacheKey = $"lyrics:{artistName}:{trackName}:{albumName}:{durationSeconds}";
@@ -112,7 +120,7 @@ public class LrclibService
var artistCountBonus = resultArtistCount == expectedArtistCount ? 50.0 : 0.0;
// Duration match (within 5 seconds is good)
var durationDiff = Math.Abs(result.Duration - durationSeconds);
var durationDiff = result.Duration.HasValue ? Math.Abs(result.Duration.Value - durationSeconds) : 999;
var durationScore = durationDiff <= 5 ? 100.0 : Math.Max(0, 100 - (durationDiff * 2));
// Bonus for having synced lyrics (prefer synced over plain)
@@ -143,7 +151,7 @@ public class LrclibService
TrackName = bestMatch.TrackName ?? trackName,
ArtistName = bestMatch.ArtistName ?? artistName,
AlbumName = bestMatch.AlbumName ?? albumName,
Duration = (int)Math.Round(bestMatch.Duration),
Duration = bestMatch.Duration.HasValue ? (int)Math.Round(bestMatch.Duration.Value) : durationSeconds,
Instrumental = bestMatch.Instrumental,
PlainLyrics = bestMatch.PlainLyrics,
SyncedLyrics = bestMatch.SyncedLyrics
@@ -192,7 +200,7 @@ public class LrclibService
TrackName = lyrics.TrackName ?? trackName,
ArtistName = lyrics.ArtistName ?? artistName,
AlbumName = lyrics.AlbumName ?? albumName,
Duration = (int)Math.Round(lyrics.Duration),
Duration = lyrics.Duration.HasValue ? (int)Math.Round(lyrics.Duration.Value) : durationSeconds,
Instrumental = lyrics.Instrumental,
PlainLyrics = lyrics.PlainLyrics,
SyncedLyrics = lyrics.SyncedLyrics
@@ -334,7 +342,7 @@ public class LrclibService
TrackName = lyrics.TrackName ?? trackName,
ArtistName = lyrics.ArtistName ?? artistName,
AlbumName = lyrics.AlbumName ?? albumName,
Duration = (int)Math.Round(lyrics.Duration),
Duration = lyrics.Duration.HasValue ? (int)Math.Round(lyrics.Duration.Value) : durationSeconds,
Instrumental = lyrics.Instrumental,
PlainLyrics = lyrics.PlainLyrics,
SyncedLyrics = lyrics.SyncedLyrics
@@ -390,7 +398,7 @@ public class LrclibService
TrackName = lyrics.TrackName ?? string.Empty,
ArtistName = lyrics.ArtistName ?? string.Empty,
AlbumName = lyrics.AlbumName ?? string.Empty,
Duration = (int)Math.Round(lyrics.Duration),
Duration = lyrics.Duration.HasValue ? (int)Math.Round(lyrics.Duration.Value) : 0,
Instrumental = lyrics.Instrumental,
PlainLyrics = lyrics.PlainLyrics,
SyncedLyrics = lyrics.SyncedLyrics
@@ -419,7 +427,7 @@ public class LrclibService
public string? TrackName { get; set; }
public string? ArtistName { get; set; }
public string? AlbumName { get; set; }
public double Duration { get; set; }
public double? Duration { get; set; }
public bool Instrumental { get; set; }
public string? PlainLyrics { get; set; }
public string? SyncedLyrics { get; set; }

View File

@@ -386,7 +386,7 @@ public class SquidWTFMetadataService : IMusicMetadataService
if (artistSource == null)
{
_logger.LogWarning("Could not find artist data in response. Response keys: {Keys}",
_logger.LogDebug("Could not find artist data in response. Response keys: {Keys}",
string.Join(", ", result.RootElement.EnumerateObject().Select(p => p.Name)));
return null;
}