Skip to content

Commit

Permalink
chore: error for oauth
Browse files Browse the repository at this point in the history
  • Loading branch information
appflowy committed Jan 22, 2024
1 parent dd412d9 commit 688093c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 14 additions & 4 deletions libs/app_error/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ pub enum AppError {
#[error("{0}")]
OAuthError(String),

#[error("{0}")]
BadRequest(String),

#[error("{0}")]
UserAlreadyRegistered(String),

#[error("Missing Payload:{0}")]
MissingPayload(String),

Expand Down Expand Up @@ -128,6 +134,8 @@ impl AppError {
AppError::InvalidEmail(_) => ErrorCode::InvalidEmail,
AppError::InvalidPassword(_) => ErrorCode::InvalidPassword,
AppError::OAuthError(_) => ErrorCode::OAuthError,
AppError::BadRequest(_) => ErrorCode::InvalidRequest,
AppError::UserAlreadyRegistered(_) => ErrorCode::RecordAlreadyExists,
AppError::MissingPayload(_) => ErrorCode::MissingPayload,
AppError::DBError(_) => ErrorCode::DBError,
AppError::OpenError(_) => ErrorCode::OpenError,
Expand Down Expand Up @@ -197,14 +205,16 @@ impl From<crate::gotrue::GoTrueError> for AppError {
GoTrueError::Connect(msg) => AppError::Connect(msg),
GoTrueError::RequestTimeout(msg) => AppError::RequestTimeout(msg),
GoTrueError::InvalidRequest(msg) => AppError::InvalidRequest(msg),
GoTrueError::ClientError(err) => AppError::OAuthError(err.to_string()),
GoTrueError::ClientError(err) => AppError::BadRequest(err.to_string()),
GoTrueError::Auth(err) => AppError::OAuthError(err),
GoTrueError::Internal(err) => match (err.code, err.msg.as_str()) {
(400, m) if m.starts_with("oauth error") => AppError::OAuthError(err.msg),
(400, m) if m.starts_with("User already registered") => AppError::OAuthError(err.msg),
(400, m) if m.starts_with("oauth error") => AppError::BadRequest(err.msg),
(400, m) if m.starts_with("User already registered") => {
AppError::UserAlreadyRegistered(err.msg)
},
(401, _) => AppError::OAuthError(err.msg),
(422, _) => AppError::InvalidRequest(err.msg),
_ => AppError::OAuthError(err.to_string()),
_ => AppError::Internal(err.into()),
},
GoTrueError::Unhandled(err) => AppError::Internal(err),
GoTrueError::NotLoggedIn(msg) => AppError::NotLoggedIn(msg),
Expand Down
1 change: 1 addition & 0 deletions libs/client-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ url = "2.5.0"
mime = "0.3.17"
tokio-stream = { version = "0.1.14" }
realtime-entity = { workspace = true }
chrono = "0.4"

collab = { version = "0.1.0", optional = true }
collab-entity = { version = "0.1.0" }
Expand Down
5 changes: 1 addition & 4 deletions libs/client-api/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1116,10 +1116,7 @@ impl Client {
let expires_at = self.token_expires_at()?;

// Refresh token if it's about to expire
let time_now_sec = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs() as i64;
let time_now_sec = chrono::Local::now().timestamp();
if time_now_sec + 10 > expires_at {
// Add 10 seconds buffer
self.refresh_token().await?;
Expand Down

0 comments on commit 688093c

Please sign in to comment.