fix: Fixed the issue where Netease Cloud Music did not throw the correct exception when a song could not be found.

This commit is contained in:
real-zony 2023-04-17 01:01:16 +08:00
parent f935f07609
commit ab5f79bd50
5 changed files with 19 additions and 8 deletions

View File

@ -41,7 +41,7 @@ namespace ZonyLrcTools.Common.Album.NetEase
true,
_defaultOption);
if (searchResult is not { StatusCode: 200 } || searchResult.Items?.SongCount <= 0)
if (searchResult is not { StatusCode: 200 } || searchResult.Items is not { SongCount: > 0 })
{
throw new ErrorCodeException(ErrorCodes.NoMatchingSong);
}

View File

@ -107,7 +107,6 @@ public class LyricsDownloader : ILyricsDownloader, ISingletonDependency
{
_logger.LogWarningInfo(ex);
info.IsSuccessful = false;
throw;
}
catch (Exception ex)
{

View File

@ -4,12 +4,17 @@ namespace ZonyLrcTools.Common.Lyrics.Providers.NetEase.JsonModel
{
public class SongSearchResponse
{
[JsonProperty("result")] public InnerListItemModel Items { get; set; }
[JsonProperty("result")] public InnerListItemModel? Items { get; set; }
[JsonProperty("code")] public int StatusCode { get; set; }
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)
{
@ -27,7 +32,7 @@ namespace ZonyLrcTools.Common.Lyrics.Providers.NetEase.JsonModel
public class InnerListItemModel
{
[JsonProperty("songs")] public IList<SongModel> SongItems { get; set; }
[JsonProperty("songs")] public IList<SongModel>? SongItems { get; set; }
[JsonProperty("songCount")] public int SongCount { get; set; }
}

View File

@ -21,7 +21,6 @@ namespace ZonyLrcTools.Common.Lyrics.Providers.NetEase
private const string NetEaseGetLyricUrl = @"https://music.163.com/weapi/song/lyric?csrf_token=";
private const string NetEaseRequestReferer = @"https://music.163.com";
private const string NetEaseRequestContentType = @"application/x-www-form-urlencoded";
public NetEaseLyricsProvider(IWarpHttpClient warpHttpClient,
ILyricsItemCollectionFactory lyricsItemCollectionFactory,
@ -49,7 +48,7 @@ namespace ZonyLrcTools.Common.Lyrics.Providers.NetEase
ValidateSongSearchResponse(searchResult, args);
return await _warpHttpClient.PostAsync(NetEaseGetLyricUrl,
return await _warpHttpClient.PostAsync(NetEaseGetLyricUrl,
requestOption: request =>
{
request.Headers.Referrer = new Uri(NetEaseRequestReferer);
@ -87,7 +86,7 @@ namespace ZonyLrcTools.Common.Lyrics.Providers.NetEase
throw new ErrorCodeException(ErrorCodes.TheReturnValueIsIllegal, attachObj: args);
}
if (response.Items?.SongCount <= 0)
if (response.Items is not { SongCount: > 0 })
{
throw new ErrorCodeException(ErrorCodes.NoMatchingSong, attachObj: args);
}

View File

@ -6,6 +6,7 @@ using Microsoft.Extensions.Options;
using Shouldly;
using Xunit;
using ZonyLrcTools.Common.Configuration;
using ZonyLrcTools.Common.Infrastructure.Exceptions;
using ZonyLrcTools.Common.Lyrics;
namespace ZonyLrcTools.Tests.Infrastructure.Lyrics
@ -105,12 +106,19 @@ namespace ZonyLrcTools.Tests.Infrastructure.Lyrics
var lyric = await _lyricsProvider.DownloadAsync("橄榄树", "苏曼");
lyric.ToString().ShouldNotBeNullOrEmpty();
}
[Fact]
public async Task DownloadAsync_Issue133_Test()
{
var lyric = await _lyricsProvider.DownloadAsync("Everything", "Yinyues");
lyric.ToString().ShouldNotBeNullOrEmpty();
}
[Fact]
public async Task DownloadAsync_NullException_Test()
{
var result = await Should.ThrowAsync<ErrorCodeException>(_lyricsProvider.DownloadAsync("創世記", "りりィ").AsTask);
result.ErrorCode.ShouldBe(ErrorCodes.NoMatchingSong);
}
}
}