From 5545ff5d649edf32efcf25ecf5c33cf3ba0b894e Mon Sep 17 00:00:00 2001 From: Nathan Gill Date: Fri, 30 Jan 2026 17:18:48 +0000 Subject: [PATCH] collapse error enum into categories --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/auth.rs | 14 +++++++------- src/error.rs | 47 +++++++++++++++------------------------------ src/main.rs | 30 +++++++++++++++++++++++++++-- src/parser/error.rs | 4 ++-- 6 files changed, 61 insertions(+), 42 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 56def69..9479522 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2818,6 +2818,7 @@ dependencies = [ "jacquard-oauth", "keyring", "onyx-lexicons", + "owo-colors", "serde", "serde_json", "thiserror 2.0.18", @@ -2924,6 +2925,12 @@ dependencies = [ "syn 2.0.114", ] +[[package]] +name = "owo-colors" +version = "4.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c6901729fa79e91a0913333229e9ca5dc725089d1c363b2f4b4760709dc4a52" + [[package]] name = "p256" version = "0.13.2" diff --git a/Cargo.toml b/Cargo.toml index 6eb94bf..34d1cc0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ jacquard-identity = { version = "0.9.5", features = ["dns"] } jacquard-oauth = { version = "0.9.6", features = ["browser-open", "loopback"] } keyring = { version = "3.6.3", features = ["linux-native-sync-persistent", "apple-native", "windows-native", "crypto-rust", "vendored"] } onyx-lexicons = { path = "crates/lexicons" } +owo-colors = "4.2.3" serde = "1.0.228" serde_json = "1.0.149" thiserror = "2.0.18" diff --git a/src/auth.rs b/src/auth.rs index 0c71809..95c64f4 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -289,19 +289,19 @@ impl HttpClient for GenericSession { GenericSession::KeyringOAuth(session) => session .send_http(request) .await - .map_err(|e| OnyxError::Identity(e.to_string())), + .map_err(|e| OnyxError::Auth(e.to_string())), GenericSession::FileOAuth(session) => session .send_http(request) .await - .map_err(|e| OnyxError::Identity(e.to_string())), + .map_err(|e| OnyxError::Auth(e.to_string())), GenericSession::KeyringPassword(session) => session .send_http(request) .await - .map_err(|e| OnyxError::Identity(e.to_string())), + .map_err(|e| OnyxError::Auth(e.to_string())), GenericSession::FilePassword(session) => session .send_http(request) .await - .map_err(|e| OnyxError::Identity(e.to_string())), + .map_err(|e| OnyxError::Auth(e.to_string())), } } } @@ -645,7 +645,7 @@ impl Authenticator { let session = match self.auth_store.get_session()? { Some(s) => s, None => { - return Err(OnyxError::AuthStore("not logged in".to_string())); + return Err(OnyxError::Auth("not logged in".to_string())); } }; @@ -710,7 +710,7 @@ impl Authenticator { let session = match self.auth_store.get_session()? { Some(s) => s, None => { - return Err(OnyxError::AuthStore("not logged in".to_string())); + return Err(OnyxError::Auth("not logged in".to_string())); } }; @@ -732,7 +732,7 @@ impl Authenticator { if let Some(session) = session { Ok(session) } else { - Err(OnyxError::AuthStore("not logged in".to_string())) + Err(OnyxError::Auth("not logged in".to_string())) } } diff --git a/src/error.rs b/src/error.rs index 2aaaeca..a058367 100644 --- a/src/error.rs +++ b/src/error.rs @@ -21,34 +21,16 @@ impl MapErrExt for Result { #[derive(Debug, Error)] pub enum OnyxError { - #[error("auth store error: {0}")] - AuthStore(String), + #[error("auth: {0}")] + Auth(String), - #[error("session store error: {0}")] - SessionStore(String), - - #[error("io error: {0}")] + #[error("io: {0}")] Io(String), - #[error("serde error: {0}")] - Serde(String), - - #[error("identity error: {0}")] - Identity(String), - - #[error("oauth error: {0}")] - OAuthError(String), - - #[error("client error: {0}")] - ClientError(String), - - #[error("agent error: {0}")] - AgentError(String), - - #[error("parser error: {0}")] - ParserError(String), + #[error("parse: {0}")] + Parse(String), - #[error(transparent)] + #[error("unknown: {0}")] Other(#[from] Box), } @@ -66,7 +48,7 @@ impl From for OnyxError { impl From for OnyxError { fn from(err: SessionStoreError) -> Self { - OnyxError::SessionStore(err.to_string()) + OnyxError::Auth(err.to_string()) } } @@ -78,36 +60,39 @@ impl From for OnyxError { impl From for OnyxError { fn from(err: serde_json::Error) -> Self { - OnyxError::Serde(err.to_string()) + OnyxError::Parse(err.to_string()) } } impl From for OnyxError { fn from(err: IdentityError) -> Self { - OnyxError::Identity(err.to_string()) + OnyxError::Other(err.to_string().into()) } } impl From for OnyxError { fn from(err: OAuthError) -> Self { - OnyxError::OAuthError(err.to_string()) + OnyxError::Auth(err.to_string()) } } impl From for OnyxError { fn from(err: ClientError) -> Self { - OnyxError::ClientError(err.to_string()) + OnyxError::Other(err.to_string().into()) } } impl From for OnyxError { fn from(err: AgentError) -> Self { - OnyxError::AgentError(err.to_string()) + OnyxError::Other(err.to_string().into()) } } impl From for OnyxError { fn from(err: ParserError) -> Self { - OnyxError::ParserError(err.to_string()) + match err { + ParserError::Io(e) => OnyxError::Io(e.to_string()), + _ => OnyxError::Parse(err.to_string()), + } } } diff --git a/src/main.rs b/src/main.rs index bb24c85..90c61ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use owo_colors::OwoColorize; use serde::{Deserialize, Serialize}; use std::path::PathBuf; @@ -179,8 +180,7 @@ fn generate_client_version() -> String { format!("v{}", env!("CARGO_PKG_VERSION")) } -#[tokio::main] -async fn main() -> Result<(), OnyxError> { +async fn run_onyx() -> Result<(), OnyxError> { let mut matches = get_command().get_matches(); let args = Args::from_arg_matches_mut(&mut matches).unwrap(); @@ -275,3 +275,29 @@ async fn main() -> Result<(), OnyxError> { Ok(()) } + +fn print_error(e: &OnyxError) { + println!("{} {}", "error:".red().bold(), e); +} + +fn handle_error(e: OnyxError) { + match e { + OnyxError::Auth(_) => { + print_error(&e); + println!( + "{} try logging in with '{}'", + "hint:".green().bold(), + "onyx auth login".cyan().bold() + ); + } + _ => print_error(&e), + } +} + +#[tokio::main] +async fn main() { + if let Err(e) = run_onyx().await { + handle_error(e); + std::process::exit(1); + } +} diff --git a/src/parser/error.rs b/src/parser/error.rs index 1dbca7e..590113c 100644 --- a/src/parser/error.rs +++ b/src/parser/error.rs @@ -2,10 +2,10 @@ use thiserror::Error; #[derive(Debug, Error)] pub enum ParserError { - #[error("io error: {0}")] + #[error("io: {0}")] Io(#[from] std::io::Error), - #[error("syntax error: {0}")] + #[error("syntax: {0}")] Syntax(String), #[error("{0}")] -- 2.51.2