diff --git a/library/src/lib.rs b/library/src/lib.rs index b0e081e..8db8ea3 100644 --- a/library/src/lib.rs +++ b/library/src/lib.rs @@ -5,5 +5,4 @@ pub mod resp; pub mod middleware; pub mod token; pub mod cache; -pub mod social; -pub mod errors; \ No newline at end of file +pub mod social; \ No newline at end of file diff --git a/library/src/middleware/req_log.rs b/library/src/middleware/req_log.rs index 8e8c5eb..72ea8d3 100644 --- a/library/src/middleware/req_log.rs +++ b/library/src/middleware/req_log.rs @@ -90,7 +90,7 @@ async fn drain_body(request: Request, next: Next) -> Result<(Response, Option v.to_bytes(), Err(err) => { tracing::error!(error = ?err, "err parse request body"); - return Err(ResErr::ErrSystem(None)); + return Err(ResErr::default()); } }; diff --git a/library/src/resp/response.rs b/library/src/resp/response.rs index d99f312..2331b02 100644 --- a/library/src/resp/response.rs +++ b/library/src/resp/response.rs @@ -112,68 +112,83 @@ impl IntoResponse for ResErr { } } -// impl Display for ResErr { -// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { -// // write!(f, "{:?}", self.message) -// match self { -// Error(value, message) => write!(f, "{} - {:?}", value, message), -// ErrParams(message) -// | ErrAuth(message) -// | ErrPerm(message) -// | ErrNotFound(message) -// | ErrSystem(message) -// | ErrData(message) -// | ErrService(message) -// | ErrSocial(message) => write!(f, "{:?}", message), -// } -// } -// } +impl Display for ResErr { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + // write!(f, "{:?}", self.message) + match self { + Error(value, message) => write!(f, "{} - {:?}", value, message), + ErrParams(message) + | ErrAuth(message) + | ErrPerm(message) + | ErrNotFound(message) + | ErrSystem(message) + | ErrData(message) + | ErrService(message) + | ErrSocial(message) => write!(f, "{:?}", message), + } + } +} -// impl StdError for ResErr { -// fn cause(&self) -> Option<&dyn StdError> { -// match self { -// Error(_, _) => None, -// ErrParams(_) -// | ErrAuth(_) -// | ErrPerm(_) -// | ErrNotFound(_) -// | ErrSystem(_) -// | ErrData(_) -// | ErrService(_) -// | ErrSocial(_) => None, -// } -// } - -// fn source(&self) -> Option<&(dyn StdError + 'static)> { -// None -// } - -// fn description(&self) -> &str { -// match self { -// Error(value, message) => &format!("Error, {} - {}", value, message), -// ErrParams(message) => &format!("ErrParams, {}", message), -// ErrAuth(message) => &format!("ErrAuth, {}", message), -// ErrPerm(message) => &format!("ErrPerm, {}", message), -// ErrNotFound(message) => &format!("ErrNotFound, {}", message), -// ErrSystem(message) => &format!("ErrSystem, {}", message), -// ErrData(message) => &format!("ErrData, {}", message), -// ErrService(message) => &format!("ErrService, {}", message), -// ErrSocial(message) => &format!("ErrSocial, {}", message), -// } -// } - -// fn provide<'a>(&'a self, request: &mut std::error::Request<'a>) { -// match self { -// ResErr::ErrSystem(e) => request.provide_ref(e), -// ResErr::ErrData(e) => request.provide_ref(e), -// ResErr::ErrService(e) => request.provide_ref(e), -// ResErr::ErrSocial(e) => request.provide_ref(e), -// ResErr::ErrNotFound(e) => request.provide_ref(e), -// ResErr::ErrPerm(e) => request.provide_ref(e), -// ResErr::ErrAuth(e) => request.provide_ref(e), -// ResErr::ErrParams(e) => request.provide_ref(e), -// ResErr::Error(_, e) => request.provide_ref(e), -// } -// } +impl StdError for ResErr { + fn source(&self) -> Option<&(dyn StdError + 'static)> { + self.source() + } +} + +impl From for ResErr { + fn from(value: String) -> Self { + ErrSystem(Some(value)) + } +} + +impl From<&str> for ResErr { + fn from(value: &str) -> Self { + ResErr::from(value.to_string()) + } +} + +impl Default for ResErr { + fn default() -> Self { + ErrSystem(None) + } +} + +impl ResErr { + pub fn error(code: i32, msg: T) -> Self where T: Into { + Error(code, msg.into()) + } + + pub fn params(msg: T) -> Self where T: Into { + ErrParams(Some(msg.into())) + } + + pub fn auth(msg: T) -> Self where T: Into { + ErrAuth(Some(msg.into())) + } + + pub fn perm(msg: T) -> Self where T: Into { + ErrPerm(Some(msg.into())) + } + + pub fn not_found(msg: T) -> Self where T: Into { + ErrNotFound(Some(msg.into())) + } + + pub fn system(msg: T) -> Self where T: Into { + ErrSystem(Some(msg.into())) + } + + pub fn data(msg: T) -> Self where T: Into { + ErrData(Some(msg.into())) + } + + pub fn service(msg: T) -> Self where T: Into { + ErrService(Some(msg.into())) + } + + pub fn social(msg: T) -> Self where T: Into { + ErrSocial(Some(msg.into())) + } +} pub type ResResult = std::result::Result; diff --git a/library/src/social/google.rs b/library/src/social/google.rs index 99d07a3..b2a56ae 100644 --- a/library/src/social/google.rs +++ b/library/src/social/google.rs @@ -5,7 +5,7 @@ use jsonwebtoken::{decode, errors::ErrorKind, Algorithm, DecodingKey, TokenData, use reqwest::Client; use serde_json::Value; -use crate::{errors::MessageError, resp::response::ResErr}; +use crate::resp::response::ResErr; use super::SocialResult; @@ -122,12 +122,12 @@ async fn verify_id_token(id_token: &str) -> SocialResult { // 检查是否找到了有效的kid if kid.is_none() { - return Err(Box::new(MessageError::from("校验Token失败,未找到有效的kid"))); + return Err(Box::new(ResErr::social("校验Token失败,未找到有效的kid"))); } let kid = kid.unwrap(); // 根据kid找到正确的公钥 - let key = public_keys.get(&kid).ok_or_else(|| Box::new(MessageError::from("校验Token失败,未找到正确的公钥")))?; + let key = public_keys.get(&kid).ok_or_else(|| Box::new(ResErr::social("校验Token失败,未找到正确的公钥")))?; // 验证Token let mut validation: Validation = Validation::new(Algorithm::RS256); @@ -140,7 +140,7 @@ async fn verify_id_token(id_token: &str) -> SocialResult { // 校验有效期 if google_jwt_profile.exp < Utc::now().timestamp() { - return Err(Box::new(MessageError::from("校验Token失败,token有效期无效"))); + return Err(Box::new(ResErr::social("校验Token失败,token有效期无效"))); } // 校验签发时间 // if google_jwt_profile.iat > Utc::now().timestamp() { @@ -150,7 +150,7 @@ async fn verify_id_token(id_token: &str) -> SocialResult { // } // 校验iss字段 if google_jwt_profile.iss != "accounts.google.com" && google_jwt_profile.iss != "https://accounts.google.com" { - return Err(Box::new(MessageError::from("校验Token失败,token签发人非法"))); + return Err(Box::new(ResErr::social("校验Token失败,token签发人非法"))); } Ok(google_jwt_profile)