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, true,
_defaultOption); _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); throw new ErrorCodeException(ErrorCodes.NoMatchingSong);
} }

View File

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

View File

@ -4,12 +4,17 @@ namespace ZonyLrcTools.Common.Lyrics.Providers.NetEase.JsonModel
{ {
public class SongSearchResponse public class SongSearchResponse
{ {
[JsonProperty("result")] public InnerListItemModel Items { get; set; } [JsonProperty("result")] public InnerListItemModel? Items { get; set; }
[JsonProperty("code")] public int StatusCode { get; set; } [JsonProperty("code")] public int StatusCode { get; set; }
public int GetFirstMatchSongId(string songName, long? duration) 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); var perfectMatch = Items.SongItems.FirstOrDefault(x => x.Name == songName);
if (perfectMatch != null) if (perfectMatch != null)
{ {
@ -27,7 +32,7 @@ namespace ZonyLrcTools.Common.Lyrics.Providers.NetEase.JsonModel
public class InnerListItemModel 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; } [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 NetEaseGetLyricUrl = @"https://music.163.com/weapi/song/lyric?csrf_token=";
private const string NetEaseRequestReferer = @"https://music.163.com"; private const string NetEaseRequestReferer = @"https://music.163.com";
private const string NetEaseRequestContentType = @"application/x-www-form-urlencoded";
public NetEaseLyricsProvider(IWarpHttpClient warpHttpClient, public NetEaseLyricsProvider(IWarpHttpClient warpHttpClient,
ILyricsItemCollectionFactory lyricsItemCollectionFactory, ILyricsItemCollectionFactory lyricsItemCollectionFactory,
@ -49,7 +48,7 @@ namespace ZonyLrcTools.Common.Lyrics.Providers.NetEase
ValidateSongSearchResponse(searchResult, args); ValidateSongSearchResponse(searchResult, args);
return await _warpHttpClient.PostAsync(NetEaseGetLyricUrl, return await _warpHttpClient.PostAsync(NetEaseGetLyricUrl,
requestOption: request => requestOption: request =>
{ {
request.Headers.Referrer = new Uri(NetEaseRequestReferer); request.Headers.Referrer = new Uri(NetEaseRequestReferer);
@ -87,7 +86,7 @@ namespace ZonyLrcTools.Common.Lyrics.Providers.NetEase
throw new ErrorCodeException(ErrorCodes.TheReturnValueIsIllegal, attachObj: args); 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); throw new ErrorCodeException(ErrorCodes.NoMatchingSong, attachObj: args);
} }

View File

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