From 32e8407ed7a4a525d553fae35d4d72169dfccc05 Mon Sep 17 00:00:00 2001 From: xfy Date: Mon, 1 Jun 2026 17:59:12 +0800 Subject: [PATCH] fix: use Debug format {:?} for database errors tokio-postgres Display only shows 'db error', use {:?} for full error chain --- src/api/auth.rs | 22 +++++++------- src/api/posts.rs | 74 ++++++++++++++++++++++++------------------------ 2 files changed, 48 insertions(+), 48 deletions(-) diff --git a/src/api/auth.rs b/src/api/auth.rs index 0548d95..9cd3110 100644 --- a/src/api/auth.rs +++ b/src/api/auth.rs @@ -92,7 +92,7 @@ pub async fn register( .get() .await .map_err(|e| { - tracing::error!("Register DB connection failed: {}", e); + tracing::error!("Register DB connection failed: {:?}", e); ServerFnError::new(format!("数据库连接失败: {}", e)) })?; @@ -100,7 +100,7 @@ pub async fn register( .query_one("SELECT COUNT(*) FROM users WHERE role = 'admin'", &[]) .await .map_err(|e| { - tracing::error!("Register admin count query failed: {}", e); + tracing::error!("Register admin count query failed: {:?}", e); ServerFnError::new(format!("查询失败: {}", e)) })? .get(0); @@ -115,7 +115,7 @@ pub async fn register( let password_hash = password::hash_password(&password) .map_err(|e| { - tracing::error!("Register password hash failed: {}", e); + tracing::error!("Register password hash failed: {:?}", e); ServerFnError::new(format!("密码哈希失败: {}", e)) })?; @@ -156,7 +156,7 @@ pub async fn login( .get() .await .map_err(|e| { - tracing::error!("Login DB connection failed: {}", e); + tracing::error!("Login DB connection failed: {:?}", e); ServerFnError::new(format!("数据库连接失败: {}", e)) })?; @@ -176,7 +176,7 @@ pub async fn login( }); } Err(e) => { - tracing::error!("Login user query failed: {}", e); + tracing::error!("Login user query failed: {:?}", e); return Err(ServerFnError::new(format!("查询失败: {}", e))); } }; @@ -184,7 +184,7 @@ pub async fn login( let password_hash: String = row.get("password_hash"); let valid = password::verify_password(&password, &password_hash) .map_err(|e| { - tracing::error!("Login password verify failed: {}", e); + tracing::error!("Login password verify failed: {:?}", e); ServerFnError::new(format!("密码验证失败: {}", e)) })?; @@ -207,7 +207,7 @@ pub async fn login( ) .await .map_err(|e| { - tracing::error!("Login session insert failed: {}", e); + tracing::error!("Login session insert failed: {:?}", e); ServerFnError::new(format!("创建 session 失败: {}", e)) })?; @@ -246,7 +246,7 @@ pub async fn logout() -> Result { .get() .await .map_err(|e| { - tracing::error!("Logout DB connection failed: {}", e); + tracing::error!("Logout DB connection failed: {:?}", e); ServerFnError::new(format!("数据库连接失败: {}", e)) })?; @@ -266,7 +266,7 @@ pub async fn logout() -> Result { .execute("DELETE FROM sessions WHERE token = $1", &[&t]) .await .map_err(|e| { - tracing::error!("Logout session delete failed: {}", e); + tracing::error!("Logout session delete failed: {:?}", e); ServerFnError::new(format!("删除 session 失败: {}", e)) })?; } @@ -305,7 +305,7 @@ pub async fn get_current_user() -> Result { .get() .await .map_err(|e| { - tracing::error!("GetCurrentUser DB connection failed: {}", e); + tracing::error!("GetCurrentUser DB connection failed: {:?}", e); ServerFnError::new(format!("数据库连接失败: {}", e)) })?; @@ -319,7 +319,7 @@ pub async fn get_current_user() -> Result { ) .await .map_err(|e| { - tracing::error!("GetCurrentUser session query failed: {}", e); + tracing::error!("GetCurrentUser session query failed: {:?}", e); ServerFnError::new(format!("查询失败: {}", e)) })?; diff --git a/src/api/posts.rs b/src/api/posts.rs index e584ed4..f1d0e8a 100644 --- a/src/api/posts.rs +++ b/src/api/posts.rs @@ -49,7 +49,7 @@ async fn get_current_admin_user() -> Result { .get() .await .map_err(|e| { - tracing::error!("DB connection failed: {}", e); + tracing::error!("DB connection failed: {:?}", e); ServerFnError::new(format!("数据库连接失败: {}", e)) })?; @@ -63,7 +63,7 @@ async fn get_current_admin_user() -> Result { ) .await .map_err(|e| { - tracing::error!("query failed: {}", e); + tracing::error!("query failed: {:?}", e); ServerFnError::new(format!("查询失败: {}", e)) })?; @@ -228,7 +228,7 @@ async fn set_post_tags( .execute("DELETE FROM post_tags WHERE post_id = $1", &[&post_id]) .await .map_err(|e| { - tracing::error!("delete tag links failed: {}", e); + tracing::error!("delete tag links failed: {:?}", e); ServerFnError::new(format!("删除标签关联失败: {}", e)) })?; @@ -247,7 +247,7 @@ async fn set_post_tags( ) .await .map_err(|e| { - tracing::error!("create tag failed: {}", e); + tracing::error!("create tag failed: {:?}", e); ServerFnError::new(format!("创建标签失败: {}", e)) })?; @@ -259,7 +259,7 @@ async fn set_post_tags( .query_opt("SELECT id FROM tags WHERE name = $1", &[&tag_name]) .await .map_err(|e| { - tracing::error!("query tag failed: {}", e); + tracing::error!("query tag failed: {:?}", e); ServerFnError::new(format!("查询标签失败: {}", e)) })?; row.map(|r| r.get(0)) @@ -275,7 +275,7 @@ async fn set_post_tags( ) .await .map_err(|e| { - tracing::error!("link tag failed: {}", e); + tracing::error!("link tag failed: {:?}", e); ServerFnError::new(format!("关联标签失败: {}", e)) })?; } @@ -420,7 +420,7 @@ pub async fn create_post( .get() .await .map_err(|e| { - tracing::error!("DB connection failed: {}", e); + tracing::error!("DB connection failed: {:?}", e); ServerFnError::new(format!("数据库连接失败: {}", e)) })?; @@ -439,7 +439,7 @@ pub async fn create_post( .transaction() .await .map_err(|e| { - tracing::error!("transaction start failed: {}", e); + tracing::error!("transaction start failed: {:?}", e); ServerFnError::new(format!("事务开始失败: {}", e)) })?; @@ -461,7 +461,7 @@ pub async fn create_post( ) .await .map_err(|e| { - tracing::error!("create post failed: {}", e); + tracing::error!("create post failed: {:?}", e); ServerFnError::new(format!("创建文章失败: {}", e)) })?; @@ -486,7 +486,7 @@ pub async fn create_post( ) .await .map_err(|e| { - tracing::error!("create tag failed: {}", e); + tracing::error!("create tag failed: {:?}", e); ServerFnError::new(format!("创建标签失败: {}", e)) })?; @@ -497,7 +497,7 @@ pub async fn create_post( .query_opt("SELECT id FROM tags WHERE name = $1", &[&tag_name.as_str()]) .await .map_err(|e| { - tracing::error!("query tag failed: {}", e); + tracing::error!("query tag failed: {:?}", e); ServerFnError::new(format!("查询标签失败: {}", e)) })?; row.map(|r| r.get(0)) @@ -512,7 +512,7 @@ pub async fn create_post( ) .await .map_err(|e| { - tracing::error!("link tag failed: {}", e); + tracing::error!("link tag failed: {:?}", e); ServerFnError::new(format!("关联标签失败: {}", e)) })?; } @@ -521,7 +521,7 @@ pub async fn create_post( tx.commit() .await .map_err(|e| { - tracing::error!("transaction commit failed: {}", e); + tracing::error!("transaction commit failed: {:?}", e); ServerFnError::new(format!("事务提交失败: {}", e)) })?; @@ -549,7 +549,7 @@ pub async fn update_post( .get() .await .map_err(|e| { - tracing::error!("DB connection failed: {}", e); + tracing::error!("DB connection failed: {:?}", e); ServerFnError::new(format!("数据库连接失败: {}", e)) })?; @@ -597,7 +597,7 @@ pub async fn update_post( .transaction() .await .map_err(|e| { - tracing::error!("transaction start failed: {}", e); + tracing::error!("transaction start failed: {:?}", e); ServerFnError::new(format!("事务开始失败: {}", e)) })?; @@ -609,7 +609,7 @@ pub async fn update_post( ) .await .map_err(|e| { - tracing::error!("query failed: {}", e); + tracing::error!("query failed: {:?}", e); ServerFnError::new(format!("查询失败: {}", e)) })?; @@ -649,7 +649,7 @@ pub async fn update_post( ) .await .map_err(|e| { - tracing::error!("update post failed: {}", e); + tracing::error!("update post failed: {:?}", e); ServerFnError::new(format!("更新文章失败: {}", e)) })?; @@ -663,7 +663,7 @@ pub async fn update_post( tx.execute("DELETE FROM post_tags WHERE post_id = $1", &[&post_id]) .await .map_err(|e| { - tracing::error!("delete old tags failed: {}", e); + tracing::error!("delete old tags failed: {:?}", e); ServerFnError::new(format!("删除旧标签失败: {}", e)) })?; @@ -676,7 +676,7 @@ pub async fn update_post( ) .await .map_err(|e| { - tracing::error!("create tag failed: {}", e); + tracing::error!("create tag failed: {:?}", e); ServerFnError::new(format!("创建标签失败: {}", e)) })?; @@ -687,7 +687,7 @@ pub async fn update_post( .query_opt("SELECT id FROM tags WHERE name = $1", &[&tag_name.as_str()]) .await .map_err(|e| { - tracing::error!("query tag failed: {}", e); + tracing::error!("query tag failed: {:?}", e); ServerFnError::new(format!("查询标签失败: {}", e)) })?; row.map(|r| r.get(0)) @@ -702,7 +702,7 @@ pub async fn update_post( ) .await .map_err(|e| { - tracing::error!("link tag failed: {}", e); + tracing::error!("link tag failed: {:?}", e); ServerFnError::new(format!("关联标签失败: {}", e)) })?; } @@ -710,7 +710,7 @@ pub async fn update_post( tx.commit() .await .map_err(|e| { - tracing::error!("transaction commit failed: {}", e); + tracing::error!("transaction commit failed: {:?}", e); ServerFnError::new(format!("事务提交失败: {}", e)) })?; @@ -728,7 +728,7 @@ pub async fn get_post_by_slug(slug: String) -> Result Result Result { .get() .await .map_err(|e| { - tracing::error!("DB connection failed: {}", e); + tracing::error!("DB connection failed: {:?}", e); ServerFnError::new(format!("数据库连接失败: {}", e)) })?; @@ -773,7 +773,7 @@ pub async fn list_published_posts() -> Result { ) .await .map_err(|e| { - tracing::error!("query failed: {}", e); + tracing::error!("query failed: {:?}", e); ServerFnError::new(format!("查询失败: {}", e)) })?; @@ -793,7 +793,7 @@ pub async fn list_posts() -> Result { .get() .await .map_err(|e| { - tracing::error!("DB connection failed: {}", e); + tracing::error!("DB connection failed: {:?}", e); ServerFnError::new(format!("数据库连接失败: {}", e)) })?; @@ -807,7 +807,7 @@ pub async fn list_posts() -> Result { ) .await .map_err(|e| { - tracing::error!("query failed: {}", e); + tracing::error!("query failed: {:?}", e); ServerFnError::new(format!("查询失败: {}", e)) })?; @@ -827,7 +827,7 @@ pub async fn delete_post(post_id: i32) -> Result Result Result { .get() .await .map_err(|e| { - tracing::error!("DB connection failed: {}", e); + tracing::error!("DB connection failed: {:?}", e); ServerFnError::new(format!("数据库连接失败: {}", e)) })?; @@ -881,7 +881,7 @@ pub async fn list_tags() -> Result { ) .await .map_err(|e| { - tracing::error!("query failed: {}", e); + tracing::error!("query failed: {:?}", e); ServerFnError::new(format!("查询失败: {}", e)) })?; @@ -903,7 +903,7 @@ pub async fn get_posts_by_tag(tag_name: String) -> Result Result Result { .get() .await .map_err(|e| { - tracing::error!("DB connection failed: {}", e); + tracing::error!("DB connection failed: {:?}", e); ServerFnError::new(format!("数据库连接失败: {}", e)) })?; @@ -982,7 +982,7 @@ pub async fn search_posts(query: String) -> Result Result