From 0320b01d1d1de857d6ee2e2b0f095e8810417f42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E8=BF=90=E5=AE=B6?= Date: Tue, 24 Sep 2024 17:38:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9C=8D=E5=8A=A1=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 26 +++++++++++++++++++------- Cargo.toml | 1 + i18n/src/en_us.rs | 4 +++- i18n/src/message_ids.rs | 2 ++ i18n/src/zh_cn.rs | 4 +++- library/Cargo.toml | 1 + library/src/middleware/mod.rs | 3 ++- library/src/model/response.rs | 18 ++++++++++++++++++ system/Cargo.toml | 1 + 9 files changed, 50 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e17f2c0..ec74551 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -137,7 +137,7 @@ dependencies = [ "serde_urlencoded", "sync_wrapper 1.0.1", "tokio", - "tower", + "tower 0.4.13", "tower-layer", "tower-service", "tracing", @@ -180,7 +180,7 @@ dependencies = [ "mime", "pin-project-lite", "serde", - "tower", + "tower 0.4.13", "tower-layer", "tower-service", "tracing", @@ -960,7 +960,7 @@ dependencies = [ "pin-project-lite", "socket2", "tokio", - "tower", + "tower 0.4.13", "tower-service", "tracing", ] @@ -1118,6 +1118,7 @@ dependencies = [ "tokio", "tokio-cron-scheduler", "toml", + "tower 0.5.1", "tower-http", "tracing", "tracing-appender", @@ -2408,6 +2409,7 @@ dependencies = [ "sqlx", "tokio", "tokio-cron-scheduler", + "tower 0.5.1", "tower-http", "tracing", "validator", @@ -2668,6 +2670,16 @@ dependencies = [ "tracing", ] +[[package]] +name = "tower" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2873938d487c3cfb9aed7546dc9f2711d867c9f90c46b889989a2cb84eba6b4f" +dependencies = [ + "tower-layer", + "tower-service", +] + [[package]] name = "tower-http" version = "0.5.2" @@ -2687,15 +2699,15 @@ dependencies = [ [[package]] name = "tower-layer" -version = "0.3.2" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c20c8dbed6283a09604c3e69b4b7eeb54e298b8a600d4d5ecb5ad39de609f1d0" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" [[package]] name = "tower-service" -version = "0.3.2" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" [[package]] name = "tracing" diff --git a/Cargo.toml b/Cargo.toml index 741d352..aa552c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,3 +52,4 @@ proc-macro2 = "1.0.86" syn = "2.0.77" quote = "1.0.37" hyper = "1.4.1" +tower = "0.5.1" \ No newline at end of file diff --git a/i18n/src/en_us.rs b/i18n/src/en_us.rs index 55ee6ae..fa2d526 100644 --- a/i18n/src/en_us.rs +++ b/i18n/src/en_us.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use lazy_static::lazy_static; use crate::message_ids::{ - ACCOUNT_DISABLED, ACCOUNT_NO_PERMISSION, HELLO, INCORRECT_USERNAME_OR_PASSWORD, INVALID_TOKEN, VALIDATE_ACCOUNT_ID_TOKEN_REQUIRED, VALIDATE_ACCOUNT_LANG_TAG_REQUIRED, VALIDATE_ACCOUNT_NAME_REQUIRED, VALIDATE_ACCOUNT_PASSWORD_REQUIRED, VALIDATE_FEEDBACK_CONTENT_REQUIRED, VALIDATE_PAGEABLE_PAGE_REQUIRED, VALIDATE_PAGEABLE_PAGE_SIZE_REQUIRED + SERVER_INTERNAL_ERROR, ACCOUNT_DISABLED, ACCOUNT_NO_PERMISSION, HELLO, INCORRECT_USERNAME_OR_PASSWORD, INVALID_TOKEN, VALIDATE_ACCOUNT_ID_TOKEN_REQUIRED, VALIDATE_ACCOUNT_LANG_TAG_REQUIRED, VALIDATE_ACCOUNT_NAME_REQUIRED, VALIDATE_ACCOUNT_PASSWORD_REQUIRED, VALIDATE_FEEDBACK_CONTENT_REQUIRED, VALIDATE_PAGEABLE_PAGE_REQUIRED, VALIDATE_PAGEABLE_PAGE_SIZE_REQUIRED }; pub const LANGUAGE_ID: &str = "en-US"; @@ -11,6 +11,8 @@ pub const LANGUAGE_ID: &str = "en-US"; lazy_static! { pub static ref MESSAGE: HashMap<&'static str, &'static str> = { let mut map = HashMap::new(); + map.insert(SERVER_INTERNAL_ERROR, "Internal server error"); + map.insert(HELLO, "hello {}"); map.insert(ACCOUNT_DISABLED, "account is disabled"); map.insert(ACCOUNT_NO_PERMISSION, "account has no permission"); diff --git a/i18n/src/message_ids.rs b/i18n/src/message_ids.rs index ae59e9e..61a8a5e 100644 --- a/i18n/src/message_ids.rs +++ b/i18n/src/message_ids.rs @@ -1,3 +1,5 @@ +pub const SERVER_INTERNAL_ERROR: &str = "SERVER_INTERNAL_ERROR"; + pub const HELLO: &str = "HELLO"; pub const ACCOUNT_DISABLED: &str = "ACCOUNT_DISABLED"; pub const ACCOUNT_NO_PERMISSION: &str = "ACCOUNT_NO_PERMISSION"; diff --git a/i18n/src/zh_cn.rs b/i18n/src/zh_cn.rs index 3ec3a45..291b73b 100644 --- a/i18n/src/zh_cn.rs +++ b/i18n/src/zh_cn.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use lazy_static::lazy_static; use crate::message_ids::{ - ACCOUNT_DISABLED, ACCOUNT_NO_PERMISSION, HELLO, INCORRECT_USERNAME_OR_PASSWORD, INVALID_TOKEN, VALIDATE_ACCOUNT_ID_TOKEN_REQUIRED, VALIDATE_ACCOUNT_LANG_TAG_REQUIRED, VALIDATE_ACCOUNT_NAME_REQUIRED, VALIDATE_ACCOUNT_PASSWORD_REQUIRED, VALIDATE_FEEDBACK_CONTENT_REQUIRED, VALIDATE_PAGEABLE_PAGE_REQUIRED, VALIDATE_PAGEABLE_PAGE_SIZE_REQUIRED + SERVER_INTERNAL_ERROR, ACCOUNT_DISABLED, ACCOUNT_NO_PERMISSION, HELLO, INCORRECT_USERNAME_OR_PASSWORD, INVALID_TOKEN, VALIDATE_ACCOUNT_ID_TOKEN_REQUIRED, VALIDATE_ACCOUNT_LANG_TAG_REQUIRED, VALIDATE_ACCOUNT_NAME_REQUIRED, VALIDATE_ACCOUNT_PASSWORD_REQUIRED, VALIDATE_FEEDBACK_CONTENT_REQUIRED, VALIDATE_PAGEABLE_PAGE_REQUIRED, VALIDATE_PAGEABLE_PAGE_SIZE_REQUIRED }; pub const LANGUAGE_ID: &str = "zh-CN"; @@ -11,6 +11,8 @@ pub const LANGUAGE_ID: &str = "zh-CN"; lazy_static! { pub static ref MESSAGE: HashMap<&'static str, &'static str> = { let mut map = HashMap::new(); + map.insert(SERVER_INTERNAL_ERROR, "系统内部错误"); + map.insert(HELLO, "你好 {}"); map.insert(ACCOUNT_DISABLED, "账户已禁用"); map.insert(ACCOUNT_NO_PERMISSION, "账户无权限"); diff --git a/library/Cargo.toml b/library/Cargo.toml index a403d7c..5260f26 100644 --- a/library/Cargo.toml +++ b/library/Cargo.toml @@ -33,6 +33,7 @@ sha2 = { workspace = true } hex-literal = { workspace = true } tokio-cron-scheduler = { workspace = true } tower-http = { workspace = true, features = ["trace"] } +tower = { workspace = true } domain = { path = "../domain" } i18n = { path = "../i18n" } \ No newline at end of file diff --git a/library/src/middleware/mod.rs b/library/src/middleware/mod.rs index c879d3d..1a6007c 100644 --- a/library/src/middleware/mod.rs +++ b/library/src/middleware/mod.rs @@ -1,4 +1,5 @@ pub mod req_id; pub mod req_log; pub mod cors; -pub mod req_ctx; \ No newline at end of file +pub mod req_ctx; +pub mod req_error_handle; \ No newline at end of file diff --git a/library/src/model/response.rs b/library/src/model/response.rs index d253845..3f6d056 100644 --- a/library/src/model/response.rs +++ b/library/src/model/response.rs @@ -162,28 +162,46 @@ impl StdError for ResErr {} impl From for ResErr { fn from(value: String) -> Self { + tracing::error!("服务错误:{}", value); ErrService(Some(value)) } } impl From<&str> for ResErr { fn from(value: &str) -> Self { + tracing::error!("服务错误:{}", value); ResErr::from(value.to_string()) } } impl From for ResErr { fn from(value: sqlx::Error) -> Self { + tracing::error!("数据库操作错误:{}", value); ErrSqlx(Some(value.to_string())) } } impl From for ResErr { fn from(value: validator::ValidationErrors) -> Self { + tracing::error!("数据校验错误:{}", value); ErrParams(Some(value.to_string())) } } +impl From for ResErr { + fn from(value: std::io::Error) -> Self { + tracing::error!("IO错误:{}", value); + ErrService(Some(value.to_string())) + } +} + +impl From for ResErr { + fn from(value: tokio::task::JoinError) -> Self { + tracing::error!("线程池错误:{}", value); + ErrService(Some(value.to_string())) + } +} + impl Default for ResErr { fn default() -> Self { ErrService(None) diff --git a/system/Cargo.toml b/system/Cargo.toml index 6229515..71df540 100644 --- a/system/Cargo.toml +++ b/system/Cargo.toml @@ -20,6 +20,7 @@ sqlx = { workspace = true, features = ["uuid"] } moka = { workspace = true, features = ["future", "logging"] } lazy_static = { workspace = true } tokio-cron-scheduler = { workspace = true } +tower = { workspace = true } library = { path = "../library" } domain = { path = "../domain" }