From 6514bcf9c145b837f2b86ca1626461a85ed2eba0 Mon Sep 17 00:00:00 2001 From: genteure Date: Sat, 10 Dec 2022 07:59:54 +0800 Subject: [PATCH] fix(core): ignore some api error code close #418 --- .../Api/BilibiliApiResponseCodeNotZeroException.cs | 10 ++++++++++ BililiveRecorder.Core/Api/Http/HttpApiClient.cs | 8 +++----- BililiveRecorder.Core/PollyPolicy.cs | 7 ++++++- BililiveRecorder.Core/Room.cs | 6 ++++++ 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/BililiveRecorder.Core/Api/BilibiliApiResponseCodeNotZeroException.cs b/BililiveRecorder.Core/Api/BilibiliApiResponseCodeNotZeroException.cs index 543057e..5e40b67 100644 --- a/BililiveRecorder.Core/Api/BilibiliApiResponseCodeNotZeroException.cs +++ b/BililiveRecorder.Core/Api/BilibiliApiResponseCodeNotZeroException.cs @@ -5,7 +5,17 @@ namespace BililiveRecorder.Core.Api { internal class BilibiliApiResponseCodeNotZeroException : Exception { + public int? Code { get; } + public string? Body { get; } + + public BilibiliApiResponseCodeNotZeroException(int? code, string? body) : base(message: "BiliBili API Code: " + (code?.ToString() ?? "(null)") + "\n" + body) + { + this.Code = code; + this.Body = body; + } + public BilibiliApiResponseCodeNotZeroException() { } + [Obsolete] public BilibiliApiResponseCodeNotZeroException(string message) : base(message) { } public BilibiliApiResponseCodeNotZeroException(string message, Exception innerException) : base(message, innerException) { } protected BilibiliApiResponseCodeNotZeroException(SerializationInfo info, StreamingContext context) : base(info, context) { } diff --git a/BililiveRecorder.Core/Api/Http/HttpApiClient.cs b/BililiveRecorder.Core/Api/Http/HttpApiClient.cs index 81ad0b9..b70f747 100644 --- a/BililiveRecorder.Core/Api/Http/HttpApiClient.cs +++ b/BililiveRecorder.Core/Api/Http/HttpApiClient.cs @@ -71,7 +71,7 @@ namespace BililiveRecorder.Core.Api.Http private void Config_PropertyChanged(object sender, PropertyChangedEventArgs e) { - if (e.PropertyName == nameof(this.config.Cookie) || e.PropertyName == nameof(this.config.TimingApiTimeout)) + if (e.PropertyName is (nameof(this.config.Cookie)) or (nameof(this.config.TimingApiTimeout))) this.UpdateHttpClient(); } @@ -89,9 +89,7 @@ namespace BililiveRecorder.Core.Api.Http var text = await resp.Content.ReadAsStringAsync().ConfigureAwait(false); var obj = JsonConvert.DeserializeObject>(text); - if (obj?.Code != 0) - throw new BilibiliApiResponseCodeNotZeroException("Bilibili api code: " + (obj?.Code?.ToString() ?? "(null)") + "\n" + text); - return obj; + return obj?.Code != 0 ? throw new BilibiliApiResponseCodeNotZeroException(obj?.Code, text) : obj; } public async Task> GetRoomInfoAsync(int roomid) @@ -118,7 +116,7 @@ namespace BililiveRecorder.Core.Api.Http var obj = jobject.ToObject>(); if (obj?.Code != 0) - throw new BilibiliApiResponseCodeNotZeroException("Bilibili api code: " + (obj?.Code?.ToString() ?? "(null)") + "\n" + text); + throw new BilibiliApiResponseCodeNotZeroException(obj?.Code, text); obj.Data!.RawBilibiliApiJsonData = jobject["data"] as JObject; diff --git a/BililiveRecorder.Core/PollyPolicy.cs b/BililiveRecorder.Core/PollyPolicy.cs index 53be44d..0aa5452 100644 --- a/BililiveRecorder.Core/PollyPolicy.cs +++ b/BililiveRecorder.Core/PollyPolicy.cs @@ -38,7 +38,12 @@ namespace BililiveRecorder.Core this.RequestFailedCircuitBreakerPolicy = Policy .Handle() .Or() - .Or() + .Or(x => + { + return x.Code is not 19002005 // 19002005: 房间已加密 + and not 19002000 // 19002000: 房间不存在(获取初始化数据失败) + ; + }) .AdvancedCircuitBreakerAsync( failureThreshold: 0.8, samplingDuration: TimeSpan.FromSeconds(30), diff --git a/BililiveRecorder.Core/Room.cs b/BililiveRecorder.Core/Room.cs index 9ea39c2..dbcd00b 100644 --- a/BililiveRecorder.Core/Room.cs +++ b/BililiveRecorder.Core/Room.cs @@ -271,6 +271,12 @@ namespace BililiveRecorder.Core this.logger.Warning("因为硬盘空间已满,本次不再自动重试启动录制。"); return; } + else if (ex is BilibiliApiResponseCodeNotZeroException notzero && notzero.Code == 19002005) + { + // 房间已加密 + this.logger.Warning("房间已加密,无密码获取不到直播流,本次不再自动重试启动录制。"); + return; + } else { // 请求直播流出错时的重试逻辑