diff --git a/crates/bili_sync/src/bilibili/error.rs b/crates/bili_sync/src/bilibili/error.rs index 4a0b17e..4e09ca3 100644 --- a/crates/bili_sync/src/bilibili/error.rs +++ b/crates/bili_sync/src/bilibili/error.rs @@ -2,10 +2,14 @@ use thiserror::Error; #[derive(Error, Debug, Clone)] pub enum BiliError { - #[error("response missing 'code' or 'message' field, full response: {0}")] + #[error("response missing 'code' field, full response: {0}")] InvalidResponse(String), - #[error("API returned error code {0}, full response: {1}")] - ErrorResponse(i64, String), + #[error("API returned error code {code}, full response: {response}")] + ErrorResponse { + code: i64, + message: Option, + response: String, + }, #[error("risk control triggered by server, full response: {0}")] RiskControlOccurred(String), #[error("invalid HTTP response code {0}, reason: {1}")] @@ -21,4 +25,15 @@ impl BiliError { BiliError::RiskControlOccurred(_) | BiliError::VideoStreamsEmpty | BiliError::InvalidStatusCode(_, _) ) } + + pub fn is_common_error(&self) -> bool { + matches!( + self, + BiliError::ErrorResponse { + code: -503, + message, + .. + } if message.as_ref().is_some_and(|m| m == "服务暂不可用") + ) + } } diff --git a/crates/bili_sync/src/bilibili/mod.rs b/crates/bili_sync/src/bilibili/mod.rs index e4028cc..f86397b 100644 --- a/crates/bili_sync/src/bilibili/mod.rs +++ b/crates/bili_sync/src/bilibili/mod.rs @@ -60,10 +60,18 @@ impl Validate for serde_json::Value { let code = self["code"] .as_i64() .with_context(|| BiliError::InvalidResponse(self.to_string()))?; + let message = self["message"].as_str().map(ToOwned::to_owned); if code == -352 || !self["data"]["v_voucher"].is_null() { bail!(BiliError::RiskControlOccurred(self.to_string())); } - ensure!(code == 0, BiliError::ErrorResponse(code, self.to_string())); + ensure!( + code == 0, + BiliError::ErrorResponse { + code, + message, + response: self.to_string(), + } + ); Ok(self) } } diff --git a/crates/bili_sync/src/config/current.rs b/crates/bili_sync/src/config/current.rs index cf7b8f1..b1da394 100644 --- a/crates/bili_sync/src/config/current.rs +++ b/crates/bili_sync/src/config/current.rs @@ -38,6 +38,8 @@ pub struct Config { pub page_name: String, #[serde(default)] pub notifiers: Option>>, + #[serde(default)] + pub ignore_common_errors: bool, #[serde(default = "default_favorite_path")] pub favorite_default_path: String, #[serde(default = "default_collection_path")] @@ -124,6 +126,7 @@ impl Default for Config { video_name: "{{title}}".to_owned(), page_name: "{{bvid}}".to_owned(), notifiers: None, + ignore_common_errors: false, favorite_default_path: default_favorite_path(), collection_default_path: default_collection_path(), submission_default_path: default_submission_path(), diff --git a/crates/bili_sync/src/task/video_downloader.rs b/crates/bili_sync/src/task/video_downloader.rs index 8db6771..1ba8051 100644 --- a/crates/bili_sync/src/task/video_downloader.rs +++ b/crates/bili_sync/src/task/video_downloader.rs @@ -140,6 +140,7 @@ impl DownloadTaskManager { &initial_config, &cx.bili_client, format!("初始化视频下载任务失败:{:#}", err), + &err, ); None } @@ -193,6 +194,7 @@ impl DownloadTaskManager { &initial_config, &cx.bili_client, format!("重载视频下载任务失败:{:#}", err), + &err, ); None } @@ -234,6 +236,7 @@ impl DownloadTaskManager { &config, &cx.bili_client, format!("本轮凭据检查与刷新任务执行遇到错误:{:#}", e), + &e, ); } } @@ -285,6 +288,7 @@ impl DownloadTaskManager { &config, &cx.bili_client, format!("本轮视频下载任务执行遇到错误:{:#}", e), + &e, ); } } @@ -360,6 +364,7 @@ async fn download_video( config, &bili_client, format!("处理 {} 时遇到错误:{:#},跳过该视频源", display_name, e), + &e, ); if let Ok(e) = e.downcast::() && e.is_risk_control_related() diff --git a/crates/bili_sync/src/utils/notify.rs b/crates/bili_sync/src/utils/notify.rs index 05cd770..dc81521 100644 --- a/crates/bili_sync/src/utils/notify.rs +++ b/crates/bili_sync/src/utils/notify.rs @@ -1,4 +1,6 @@ -use crate::bilibili::BiliClient; +use anyhow::Error; + +use crate::bilibili::{BiliClient, BiliError}; use crate::config::Config; use crate::notifier::{Message, NotifierAllExt}; @@ -12,8 +14,17 @@ pub fn notify(config: &Config, bili_client: &BiliClient, msg: impl Into() + .is_some_and(BiliError::is_common_error) + }) + { + return; + } if let Some(notifiers) = &config.notifiers && !notifiers.is_empty() { diff --git a/crates/bili_sync/src/workflow.rs b/crates/bili_sync/src/workflow.rs index af3bef3..bd44303 100644 --- a/crates/bili_sync/src/workflow.rs +++ b/crates/bili_sync/src/workflow.rs @@ -140,7 +140,7 @@ pub async fn fetch_video_details( "获取视频 {} - {} 的详细信息失败,错误为:{:#}", &video_model.bvid, &video_model.name, e ); - if let Some(BiliError::ErrorResponse(-404, _)) = e.downcast_ref::() { + if let Some(BiliError::ErrorResponse { code: -404, .. }) = e.downcast_ref::() { let mut video_active_model: bili_sync_entity::video::ActiveModel = video_model.into(); video_active_model.valid = Set(false); video_active_model.save(connection).await?; diff --git a/web/src/lib/types.ts b/web/src/lib/types.ts index ef86087..477dd16 100644 --- a/web/src/lib/types.ts +++ b/web/src/lib/types.ts @@ -330,6 +330,7 @@ export interface Config { video_name: string; page_name: string; notifiers: Notifier[] | null; + ignore_common_errors: boolean; favorite_default_path: string; collection_default_path: string; submission_default_path: string; diff --git a/web/src/routes/settings/+page.svelte b/web/src/routes/settings/+page.svelte index 9716a1c..868be94 100644 --- a/web/src/routes/settings/+page.svelte +++ b/web/src/routes/settings/+page.svelte @@ -792,6 +792,16 @@
+
+
+ +

+ b 站接口频繁出现 -503 服务暂不可用,在通知器内通知容易刷屏,开启该选项以忽略此错误通知 +

+
+ +
+

通知器管理