diff --git a/src/api/auth.rs b/src/api/auth.rs index 6be4ff9..7da093d 100644 --- a/src/api/auth.rs +++ b/src/api/auth.rs @@ -1,4 +1,8 @@ -use crate::api::client::{ApiClient, Endpoint}; +use crate::{ + Result, + api::client::{ApiClient, Endpoint}, + error::AuthError, +}; use keyring::KeyringEntry; use serde_json::Value; @@ -7,22 +11,20 @@ pub struct Auth { } impl Auth { - pub fn new() -> Result> { + pub fn new() -> Result { let crate_id = "vimstoat"; let token_entry = KeyringEntry::try_new(crate_id)?; Ok(Self { token_entry }) } - pub async fn store_token(&self, token: &str) -> Result<(), String> { - self.token_entry.set_secret(token).await.map_err(|e| { - format!( - "{}\n\nUnderlying Details:\n{:?}\n\n💡 Hint: If you are on a minimal Linux install, you likely need to install a Secret Service provider (e.g., `sudo pacman -S gnome-keyring`).", - e, e - ) - }) + pub async fn store_token(&self, token: &str) -> Result<()> { + self.token_entry + .set_secret(token) + .await + .map_err(|e| e.into()) } - pub async fn validate_token(&self, token: &str) -> Result { + pub async fn validate_token(&self, token: &str) -> Result { let client = ApiClient::new(token.to_string()); client @@ -32,12 +34,14 @@ impl Auth { .map_err(|e| { let err_msg = e.to_string(); if err_msg.contains("401") { - "Invalid token. Please check your session token and try again.".to_string() + AuthError::InvalidToken( + "Please check your session token and try again.".to_string(), + ) + .into() } else if err_msg.contains("API GET request") { - err_msg + AuthError::RequestError(err_msg).into() } else { - "Could not connect to the server. Please check your internet connection." - .to_string() + AuthError::ServerConnectionError.into() } }) } diff --git a/src/api/client.rs b/src/api/client.rs index adaa6b9..761787f 100644 --- a/src/api/client.rs +++ b/src/api/client.rs @@ -5,6 +5,7 @@ use serde::de::DeserializeOwned; const BASE_URL: &str = "https://api.stoat.chat"; #[derive(Debug)] +#[allow(unused)] pub enum Endpoint { Config, CurrentUser, diff --git a/src/app.rs b/src/app.rs index d85723c..59857dd 100644 --- a/src/app.rs +++ b/src/app.rs @@ -10,7 +10,7 @@ pub enum AppState { InputToken, ValidatingToken, LoggedIn, - Error(String), + Error(anyhow::Error), } pub struct App { diff --git a/src/error.rs b/src/error.rs index eb5d750..8397af5 100644 --- a/src/error.rs +++ b/src/error.rs @@ -20,3 +20,24 @@ impl From for CacheError { CacheError::DbError(value) } } + +#[derive(Error, Debug)] +pub enum AuthError { + #[error("Keyring Error: {0:?}")] + KeyringError(keyring::Error), + + #[error("Invalid Token: {0}")] + InvalidToken(String), + + #[error("Could not connect to the server. Please check your internet connection.")] + ServerConnectionError, + + #[error("Request Error: {0}")] + RequestError(String), +} + +impl From for AuthError { + fn from(value: keyring::Error) -> Self { + AuthError::KeyringError(value) + } +} diff --git a/src/ui/render.rs b/src/ui/render.rs index c58799c..b56bfb3 100644 --- a/src/ui/render.rs +++ b/src/ui/render.rs @@ -8,6 +8,6 @@ pub fn render(f: &mut Frame, app: &App) { AppState::InputToken => input_token::render(f, app), AppState::ValidatingToken => validating_token::render(f), AppState::LoggedIn => server_list::render(f, app), - AppState::Error(message) => error::render(f, message), + AppState::Error(message) => error::render(f, &message.to_string()), } }