添加服务异常消息

This commit is contained in:
李运家 2024-09-24 17:38:02 +08:00
parent 65efc24115
commit 0320b01d1d
9 changed files with 50 additions and 10 deletions

26
Cargo.lock generated
View File

@ -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"

View File

@ -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"

View File

@ -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");

View File

@ -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";

View File

@ -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, "账户无权限");

View File

@ -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" }

View File

@ -1,4 +1,5 @@
pub mod req_id;
pub mod req_log;
pub mod cors;
pub mod req_ctx;
pub mod req_ctx;
pub mod req_error_handle;

View File

@ -162,28 +162,46 @@ impl StdError for ResErr {}
impl From<String> 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<sqlx::Error> for ResErr {
fn from(value: sqlx::Error) -> Self {
tracing::error!("数据库操作错误:{}", value);
ErrSqlx(Some(value.to_string()))
}
}
impl From<validator::ValidationErrors> for ResErr {
fn from(value: validator::ValidationErrors) -> Self {
tracing::error!("数据校验错误:{}", value);
ErrParams(Some(value.to_string()))
}
}
impl From<std::io::Error> for ResErr {
fn from(value: std::io::Error) -> Self {
tracing::error!("IO错误{}", value);
ErrService(Some(value.to_string()))
}
}
impl From<tokio::task::JoinError> 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)

View File

@ -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" }