diff options
| author | Owen Jacobson <owen@grimoire.ca> | 2025-11-25 21:02:25 -0500 |
|---|---|---|
| committer | Owen Jacobson <owen@grimoire.ca> | 2025-11-25 21:02:25 -0500 |
| commit | 664e3beba053aee50fc6b3cdcc6ee0dfe5e0fe1f (patch) | |
| tree | 096b997d56959dd88d099f4f96a383daa4dbc39a /src/token/app.rs | |
| parent | 91c33501a315abe04aeed54aa27388ce0ad241ce (diff) | |
| parent | 33601ef703a640b57e5bd0bf7dbd6d7ffa7377bf (diff) | |
Merge branch 'house-of-failed'
Diffstat (limited to 'src/token/app.rs')
| -rw-r--r-- | src/token/app.rs | 55 |
1 files changed, 29 insertions, 26 deletions
diff --git a/src/token/app.rs b/src/token/app.rs index 4a08877..5916e53 100644 --- a/src/token/app.rs +++ b/src/token/app.rs @@ -6,11 +6,15 @@ use futures::{ use sqlx::sqlite::SqlitePool; use super::{ - Broadcaster, Event as TokenEvent, Secret, Token, - extract::Identity, - repo::{self, Provider as _}, + Broadcaster, Event as TokenEvent, Secret, Token, extract::Identity, repo::Provider as _, +}; +use crate::{ + clock::DateTime, + db, + db::NotFound as _, + error::failed::{Failed, ResultExt as _}, + push::repo::Provider as _, }; -use crate::{clock::DateTime, db::NotFound as _, name, push::repo::Provider as _}; pub struct Tokens { db: SqlitePool, @@ -27,13 +31,15 @@ impl Tokens { secret: &Secret, used_at: &DateTime, ) -> Result<Identity, ValidateError> { - let mut tx = self.db.begin().await?; + let mut tx = self.db.begin().await.fail(db::failed::BEGIN)?; let (token, login) = tx .tokens() .validate(secret, used_at) .await - .not_found(|| ValidateError::InvalidToken)?; - tx.commit().await?; + .optional() + .fail("Failed to load token")? + .ok_or(ValidateError::InvalidToken)?; + tx.commit().await.fail(db::failed::COMMIT)?; Ok(Identity { token, login }) } @@ -66,12 +72,14 @@ impl Tokens { // matter. Supervising a stream, on the other hand, will run for a // _long_ time; if we miss the race here, we'll never actually carry out the // supervision. - let mut tx = self.db.begin().await?; + let mut tx = self.db.begin().await.fail(db::failed::BEGIN)?; tx.tokens() .require(&token) .await - .not_found(|| ValidateError::InvalidToken)?; - tx.commit().await?; + .optional() + .fail("Failed to load token")? + .ok_or(ValidateError::InvalidToken)?; + tx.commit().await.fail(db::failed::COMMIT)?; // Then construct the guarded stream. First, project both streams into // `GuardedEvent`. @@ -111,10 +119,16 @@ impl Tokens { } pub async fn logout(&self, token: &Token) -> Result<(), ValidateError> { - let mut tx = self.db.begin().await?; - tx.push().unsubscribe_token(token).await?; - tx.tokens().revoke(token).await?; - tx.commit().await?; + let mut tx = self.db.begin().await.fail(db::failed::BEGIN)?; + tx.push() + .unsubscribe_token(token) + .await + .fail("Failed to remove push subscriptions")?; + tx.tokens() + .revoke(token) + .await + .fail("Failed to revoke token")?; + tx.commit().await.fail(db::failed::COMMIT)?; self.token_events .broadcast(TokenEvent::Revoked(token.id.clone())); @@ -128,18 +142,7 @@ pub enum ValidateError { #[error("invalid token")] InvalidToken, #[error(transparent)] - Database(#[from] sqlx::Error), - #[error(transparent)] - Name(#[from] name::Error), -} - -impl From<repo::LoadError> for ValidateError { - fn from(error: repo::LoadError) -> Self { - match error { - repo::LoadError::Database(error) => error.into(), - repo::LoadError::Name(error) => error.into(), - } - } + Failed(#[from] Failed), } #[derive(Debug)] |
