fix: Fixed some unhandled exceptions.

This commit is contained in:
real-zony 2023-04-17 01:13:06 +08:00
parent ab5f79bd50
commit 788ff38be2
9 changed files with 28 additions and 10 deletions

View File

@ -18,6 +18,6 @@ namespace ZonyLrcTools.Common.Lyrics
/// <param name="sourceLyric">原始歌词数据。</param>
/// <param name="translationLyric">翻译歌词数据。</param>
/// <returns>构建完成的 <see cref="LyricsItemCollection"/> 对象。</returns>
LyricsItemCollection Build(string sourceLyric, string translationLyric);
LyricsItemCollection Build(string sourceLyric, string? translationLyric);
}
}

View File

@ -30,7 +30,7 @@ namespace ZonyLrcTools.Common.Lyrics
return lyric;
}
public LyricsItemCollection Build(string sourceLyric, string translationLyric)
public LyricsItemCollection Build(string sourceLyric, string? translationLyric)
{
var lyric = new LyricsItemCollection(_options.Provider.Lyric.Config);
if (string.IsNullOrEmpty(sourceLyric))

View File

@ -15,7 +15,7 @@ public class GetLyricsResponse
public class GetLyricsResponseInnerData
{
[JsonProperty("lrclist")] public ICollection<GetLyricsItem> Lyrics { get; set; }
[JsonProperty("lrclist")] public ICollection<GetLyricsItem>? Lyrics { get; set; }
}
public class GetLyricsItem

View File

@ -60,6 +60,11 @@ public class KuWoLyricsProvider : LyricsProvider
await ValueTask.CompletedTask;
var lyricsResponse = (GetLyricsResponse)lyricsObject;
if (lyricsResponse.Data.Lyrics == null)
{
return new LyricsItemCollection(null);
}
var items = lyricsResponse.Data.Lyrics.Select(l =>
{
var position = double.Parse(l.Position);

View File

@ -20,7 +20,7 @@ namespace ZonyLrcTools.Common.Lyrics.Providers.NetEase.JsonModel
/// 如果存在翻译歌词,则本项内容为翻译歌词。
/// </summary>
[JsonProperty("tlyric")]
public InnerLyric TranslationLyric { get; set; }
public InnerLyric? TranslationLyric { get; set; }
/// <summary>
/// 如果存在罗马音歌词,则本项内容为罗马音歌词。

View File

@ -10,11 +10,6 @@ namespace ZonyLrcTools.Common.Lyrics.Providers.NetEase.JsonModel
public int GetFirstMatchSongId(string songName, long? duration)
{
if (Items == null || Items.SongItems == null)
{
Console.Write("xx");
}
var perfectMatch = Items.SongItems.FirstOrDefault(x => x.Name == songName);
if (perfectMatch != null)
{

View File

@ -76,7 +76,7 @@ namespace ZonyLrcTools.Common.Lyrics.Providers.NetEase
return _lyricsItemCollectionFactory.Build(
json.OriginalLyric.Text,
json.TranslationLyric.Text);
json.TranslationLyric?.Text);
}
protected virtual void ValidateSongSearchResponse(SongSearchResponse response, LyricsProviderArgs args)

View File

@ -25,4 +25,13 @@ public class KuWoLyricsProviderTests : TestBase
lyric.ShouldNotBeNull();
lyric.IsPruneMusic.ShouldBeFalse();
}
[Fact]
public async Task DownloadAsync_Source_Null_Test()
{
var lyric = await _kuwoLyricsProvider.DownloadAsync("Concerto for Piano and Orchestra No. 12 in A major, K414 - 1. Allegro",
"Wolfgang Amadeus Mozart");
lyric.IsPruneMusic.ShouldBeTrue();
}
}

View File

@ -120,5 +120,14 @@ namespace ZonyLrcTools.Tests.Infrastructure.Lyrics
var result = await Should.ThrowAsync<ErrorCodeException>(_lyricsProvider.DownloadAsync("創世記", "りりィ").AsTask);
result.ErrorCode.ShouldBe(ErrorCodes.NoMatchingSong);
}
[Fact]
public async Task DownloadAsync_Source_Null_Test()
{
var lyric = await _lyricsProvider.DownloadAsync("Concerto for Piano and Orchestra No. 12 in A major, K414 - 1. Allegro",
"Wolfgang Amadeus Mozart");
lyric.IsPruneMusic.ShouldBeTrue();
}
}
}