From 0b9d7e6d6bad8ba343536d93255f4856c7c1591a Mon Sep 17 00:00:00 2001 From: "Nathan.fooo" <86001920+appflowy@users.noreply.github.com> Date: Wed, 25 Oct 2023 10:14:41 +0800 Subject: [PATCH] chore: add logs (#138) --- libs/client-api/src/http.rs | 30 +++++++++++++++++++++++------- libs/client-api/src/notify.rs | 2 +- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/libs/client-api/src/http.rs b/libs/client-api/src/http.rs index 6427a0f2..4440c7ef 100644 --- a/libs/client-api/src/http.rs +++ b/libs/client-api/src/http.rs @@ -29,10 +29,10 @@ use shared_entity::dto::workspace_dto::{ use shared_entity::error_code::url_missing_param; use shared_entity::error_code::ErrorCode; use std::sync::Arc; -use std::time::SystemTime; +use std::time::{Duration, SystemTime}; use tokio::fs::File; use tokio::io::AsyncReadExt; -use tracing::instrument; +use tracing::{event, instrument}; use url::Url; use gotrue_entity::dto::SignUpResponse::{Authenticated, NotAuthenticated}; @@ -81,7 +81,7 @@ impl Client { } #[instrument(level = "debug", skip_all, err)] - pub fn set_token(&self, token: &str) -> Result<(), AppError> { + pub fn restore_token(&self, token: &str) -> Result<(), AppError> { if token.is_empty() { return Err(AppError::new(ErrorCode::OAuthError, "Empty token")); } @@ -321,7 +321,15 @@ impl Client { pub fn token_expires_at(&self) -> Result { match &self.token.try_read() { None => Err(AppError::new(ErrorCode::Unhandled, "Failed to read token")), - Some(token) => Ok(token.as_ref().ok_or(ErrorCode::NotLoggedIn)?.expires_at), + Some(token) => Ok( + token + .as_ref() + .ok_or(AppError::new( + ErrorCode::NotLoggedIn, + "fail to get expires_at", + ))? + .expires_at, + ), } } @@ -334,12 +342,15 @@ impl Client { /// - `Err(AppError)`: An `AppError` indicating either an inability to read the token or that the user is not logged in. /// pub fn access_token(&self) -> Result { - match &self.token.try_read() { + match &self.token.try_read_for(Duration::from_secs(2)) { None => Err(AppError::new(ErrorCode::Unhandled, "Failed to read token")), Some(token) => Ok( token .as_ref() - .ok_or(ErrorCode::NotLoggedIn)? + .ok_or(AppError::new( + ErrorCode::NotLoggedIn, + "fail to get access token. Token is empty", + ))? .access_token .clone(), ), @@ -524,7 +535,10 @@ impl Client { .token .read() .as_ref() - .ok_or(AppError::new(ErrorCode::NotLoggedIn, "No access token"))? + .ok_or(AppError::new( + ErrorCode::NotLoggedIn, + "fail to refresh user token", + ))? .refresh_token .as_str() .to_owned(); @@ -538,6 +552,7 @@ impl Client { Ok(()) }, Err(err) => { + event!(tracing::Level::ERROR, "refresh token failed: {}", err); self.token.write().unset(); Err(AppError::from(err)) }, @@ -561,6 +576,7 @@ impl Client { #[instrument(level = "debug", skip_all, err)] pub async fn sign_out(&self) -> Result<(), AppError> { self.gotrue_client.logout(&self.access_token()?).await?; + self.token.write().unset(); Ok(()) } diff --git a/libs/client-api/src/notify.rs b/libs/client-api/src/notify.rs index cd3f0204..167f71d8 100644 --- a/libs/client-api/src/notify.rs +++ b/libs/client-api/src/notify.rs @@ -41,13 +41,13 @@ impl ClientToken { /// /// - `token`: The new `AccessTokenResponse` to be set. pub(crate) fn set(&mut self, new_token: AccessTokenResponse) { - tracing::trace!("Set new access token: {:?}", new_token); let is_new = match &self.token { None => true, Some(old_token) => old_token.access_token != new_token.access_token, }; self.token = Some(new_token); if is_new { + tracing::trace!("Set new access token: {:?}", self.token); let _ = self.sender.send(TokenState::Refresh); } }