From bd974cc814f9456885565bd1037cdc88cb9143be Mon Sep 17 00:00:00 2001 From: Jan Vanbuel Date: Wed, 4 Dec 2024 08:44:14 +0100 Subject: [PATCH] feat: use anyhow instead of custom errors --- Cargo.lock | 7 ++ Cargo.toml | 1 + src/airflow/client.rs | 5 +- src/airflow/client/dagruns.rs | 2 +- src/airflow/client/dags.rs | 2 +- src/airflow/client/dagstats.rs | 3 +- src/airflow/client/logs.rs | 2 +- src/airflow/client/taskinstances.rs | 3 +- src/airflow/config/mod.rs | 2 +- src/airflow/managed_services/conveyor.rs | 2 +- src/app.rs | 1 - src/app/error.rs | 136 ----------------------- src/app/model/config.rs | 4 +- src/app/model/dagruns.rs | 4 +- src/app/model/dags.rs | 8 +- src/app/model/logs.rs | 5 +- src/app/model/taskinstances.rs | 4 +- src/app/state.rs | 2 +- src/commands/config/add.rs | 2 +- src/commands/config/list.rs | 2 +- src/commands/config/managed_services.rs | 2 +- src/commands/config/model.rs | 2 +- src/commands/config/remove.rs | 2 +- src/commands/config/update.rs | 2 +- src/commands/run.rs | 3 +- src/main.rs | 2 +- 26 files changed, 41 insertions(+), 169 deletions(-) delete mode 100644 src/app/error.rs diff --git a/Cargo.lock b/Cargo.lock index 3c856f7..ddcbd35 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -109,6 +109,12 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "anyhow" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1fd03a028ef38ba2276dce7e33fcd6369c158a1bca17946c4b1b701891c1ff7" + [[package]] name = "assert-json-diff" version = "2.0.2" @@ -547,6 +553,7 @@ name = "flowrs-tui" version = "0.1.4" dependencies = [ "ansi-to-tui", + "anyhow", "backtrace", "chrono", "clap", diff --git a/Cargo.toml b/Cargo.toml index c026dbb..a90336f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ formula = "flowrs" [dependencies] ansi-to-tui = { version = "7.0.0" } +anyhow = "1.0.94" backtrace = "0.3.73" chrono = "0.4.38" clap = { version = "^4.5", features = ["derive", "env"] } diff --git a/src/airflow/client.rs b/src/airflow/client.rs index 1f4bd75..a40ff31 100644 --- a/src/airflow/client.rs +++ b/src/airflow/client.rs @@ -4,14 +4,13 @@ pub mod dagstats; pub mod logs; pub mod taskinstances; -use crate::app::error::Result; +use anyhow::Result; use std::time::Duration; use log::info; use reqwest::{Method, Url}; use crate::airflow::config::{AirflowAuth, AirflowConfig}; -use crate::app::error::{ConfigError, FlowrsError}; #[derive(Debug, Clone)] pub struct AirFlowClient { @@ -56,7 +55,7 @@ impl AirFlowClient { if let Some(token) = &token.token { return Ok(self.client.request(method, url).bearer_auth(token.trim())); } - Err(FlowrsError::ConfigError(ConfigError::NoTokenOrCmd)) + Err(anyhow::anyhow!("Token not found")) } } } diff --git a/src/airflow/client/dagruns.rs b/src/airflow/client/dagruns.rs index f874ba7..35dd797 100644 --- a/src/airflow/client/dagruns.rs +++ b/src/airflow/client/dagruns.rs @@ -2,7 +2,7 @@ use log::debug; use reqwest::{Method, Response}; use crate::airflow::model::dagrun::DagRunList; -use crate::app::error::Result; +use anyhow::Result; use super::AirFlowClient; diff --git a/src/airflow/client/dags.rs b/src/airflow/client/dags.rs index e11d4af..f9c1827 100644 --- a/src/airflow/client/dags.rs +++ b/src/airflow/client/dags.rs @@ -1,4 +1,4 @@ -use crate::app::error::Result; +use anyhow::Result; use log::error; use reqwest::{Method, Response}; diff --git a/src/airflow/client/dagstats.rs b/src/airflow/client/dagstats.rs index 9c6c97b..55aefad 100644 --- a/src/airflow/client/dagstats.rs +++ b/src/airflow/client/dagstats.rs @@ -1,5 +1,6 @@ -use crate::{airflow::model::dagstats::DagStatsResponse, app::error::Result}; +use crate::airflow::model::dagstats::DagStatsResponse; use reqwest::Method; +use anyhow::Result; use super::AirFlowClient; diff --git a/src/airflow/client/logs.rs b/src/airflow/client/logs.rs index 3905d3d..6d55893 100644 --- a/src/airflow/client/logs.rs +++ b/src/airflow/client/logs.rs @@ -1,7 +1,7 @@ use reqwest::Method; use crate::airflow::model::log::Log; -use crate::app::error::Result; +use anyhow::Result; use super::AirFlowClient; diff --git a/src/airflow/client/taskinstances.rs b/src/airflow/client/taskinstances.rs index 3680b88..d15c6e2 100644 --- a/src/airflow/client/taskinstances.rs +++ b/src/airflow/client/taskinstances.rs @@ -1,5 +1,4 @@ -use crate::app::error::Result; - +use anyhow::Result; use log::{debug, info}; use reqwest::{Method, Response}; diff --git a/src/airflow/config/mod.rs b/src/airflow/config/mod.rs index 7ab7424..35007e1 100644 --- a/src/airflow/config/mod.rs +++ b/src/airflow/config/mod.rs @@ -9,8 +9,8 @@ use serde::{Deserialize, Serialize}; use strum::EnumIter; use super::managed_services::conveyor::get_conveyor_environment_servers; -use crate::app::error::Result; use crate::CONFIG_FILE; +use anyhow::Result; #[derive(Deserialize, Serialize, Debug, Clone, PartialEq, ValueEnum, EnumIter)] pub enum ManagedService { diff --git a/src/airflow/managed_services/conveyor.rs b/src/airflow/managed_services/conveyor.rs index 2e6297b..cbdc190 100644 --- a/src/airflow/managed_services/conveyor.rs +++ b/src/airflow/managed_services/conveyor.rs @@ -1,7 +1,7 @@ use crate::airflow::config::{AirflowAuth, AirflowConfig, ManagedService, TokenCmd}; -use crate::app::error::Result; use log::info; use serde::{Deserialize, Serialize}; +use anyhow::Result; #[derive(Deserialize, Serialize, Debug, Clone)] pub struct ConveyorEnvironment { diff --git a/src/app.rs b/src/app.rs index 8ea789f..59d11d4 100644 --- a/src/app.rs +++ b/src/app.rs @@ -13,7 +13,6 @@ use worker::{Worker, WorkerMessage}; use crate::{airflow::client::AirFlowClient, ui::draw_ui}; -pub mod error; pub mod events; pub mod model; pub mod state; diff --git a/src/app/error.rs b/src/app/error.rs deleted file mode 100644 index 2a18edc..0000000 --- a/src/app/error.rs +++ /dev/null @@ -1,136 +0,0 @@ -use time::error::IndeterminateOffset; - -pub type Result = std::result::Result; - -#[derive(Debug)] -pub enum FlowrsError { - ConfigError(ConfigError), - APIError(reqwest::Error), - Serde(serde_json::Error), - Generic(String), -} - -#[derive(Debug)] -pub enum ConfigError { - Serde(ConfigSerdeError), - Input(inquire::InquireError), - IO(std::io::Error), - TokenCmdParse(std::string::FromUtf8Error), - NoTokenOrCmd, - URLParse(url::ParseError), - LogError(log::SetLoggerError), -} - -#[derive(Debug)] -pub enum ConfigSerdeError { - ConfigSerializeError(toml::ser::Error), - ConfigDeserializeError(toml::de::Error), -} - -impl std::fmt::Display for ConfigSerdeError { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - match self { - ConfigSerdeError::ConfigSerializeError(e) => write!(f, "ConfigSerializeError: {}", e), - ConfigSerdeError::ConfigDeserializeError(e) => { - write!(f, "ConfigDeserializeError: {}", e) - } - } - } -} - -impl std::fmt::Display for ConfigError { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - match self { - ConfigError::Serde(e) => write!(f, "SerdeError: {}", e), - ConfigError::Input(e) => write!(f, "InputError: {}", e), - ConfigError::IO(e) => write!(f, "IOError: {}", e), - ConfigError::TokenCmdParse(e) => write!(f, "TokenCmdParseError: {}", e), - ConfigError::URLParse(e) => write!(f, "URLParseError: {}", e), - ConfigError::LogError(e) => write!(f, "LogError: {}", e), - ConfigError::NoTokenOrCmd => { - write!(f, "No token or command to refresh token provided.") - } - } - } -} - -impl std::fmt::Display for FlowrsError { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - match self { - FlowrsError::ConfigError(e) => write!(f, "ConfigError: {}", e), - FlowrsError::APIError(e) => write!(f, "APIError: {}", e), - FlowrsError::Serde(e) => write!(f, "SerdeError: {}", e), - FlowrsError::Generic(e) => write!(f, "GenericError: {}", e), - } - } -} - -impl From for FlowrsError { - fn from(error: toml::de::Error) -> Self { - FlowrsError::ConfigError(ConfigError::Serde( - ConfigSerdeError::ConfigDeserializeError(error), - )) - } -} - -impl From for FlowrsError { - fn from(error: toml::ser::Error) -> Self { - FlowrsError::ConfigError(ConfigError::Serde(ConfigSerdeError::ConfigSerializeError( - error, - ))) - } -} - -impl From for FlowrsError { - fn from(error: inquire::InquireError) -> Self { - FlowrsError::ConfigError(ConfigError::Input(error)) - } -} - -impl From for FlowrsError { - fn from(error: std::io::Error) -> Self { - FlowrsError::ConfigError(ConfigError::IO(error)) - } -} - -impl From for FlowrsError { - fn from(error: std::string::FromUtf8Error) -> Self { - FlowrsError::ConfigError(ConfigError::TokenCmdParse(error)) - } -} - -impl From for FlowrsError { - fn from(error: reqwest::Error) -> Self { - FlowrsError::APIError(error) - } -} - -impl From for FlowrsError { - fn from(error: url::ParseError) -> Self { - FlowrsError::ConfigError(ConfigError::URLParse(error)) - } -} - -impl From for FlowrsError { - fn from(error: log::SetLoggerError) -> Self { - FlowrsError::ConfigError(ConfigError::LogError(error)) - } -} - -impl From for FlowrsError { - fn from(error: serde_json::Error) -> Self { - FlowrsError::Serde(error) - } -} - -impl From for FlowrsError { - fn from(error: String) -> Self { - FlowrsError::Generic(error) - } -} - -impl From for FlowrsError { - fn from(error: IndeterminateOffset) -> Self { - FlowrsError::Generic(error.to_string()) - } -} diff --git a/src/app/model/config.rs b/src/app/model/config.rs index 5beae13..d5be35f 100644 --- a/src/app/model/config.rs +++ b/src/app/model/config.rs @@ -12,15 +12,15 @@ use crate::app::worker::WorkerMessage; use crate::ui::constants::{ALTERNATING_ROW_COLOR, DEFAULT_STYLE}; use super::{filter::Filter, Model, StatefulTable}; -use crate::app::error::FlowrsError; use crate::ui::common::create_headers; +use anyhow::Error; pub struct ConfigModel { pub all: Vec, pub filtered: StatefulTable, pub filter: Filter, #[allow(dead_code)] - pub errors: Vec, + pub errors: Vec, } impl ConfigModel { diff --git a/src/app/model/dagruns.rs b/src/app/model/dagruns.rs index 3045f08..b8107f1 100644 --- a/src/app/model/dagruns.rs +++ b/src/app/model/dagruns.rs @@ -27,8 +27,8 @@ use super::popup::dagruns::DagRunPopUp; use super::popup::popup_area; use super::popup::{dagruns::clear::ClearDagRunPopup, dagruns::mark::MarkDagRunPopup}; use super::{filter::Filter, Model, StatefulTable}; -use crate::app::error::FlowrsError; use crate::app::worker::WorkerMessage; +use anyhow::Error; pub struct DagRunModel { pub dag_id: Option, @@ -38,7 +38,7 @@ pub struct DagRunModel { pub filter: Filter, pub marked: Vec, #[allow(dead_code)] - pub errors: Vec, + pub errors: Vec, pub popup: Option, pub commands: Option>, ticks: u32, diff --git a/src/app/model/dags.rs b/src/app/model/dags.rs index 4ccee2c..529c7d0 100644 --- a/src/app/model/dags.rs +++ b/src/app/model/dags.rs @@ -18,8 +18,8 @@ use crate::ui::constants::{AirflowStateColor, ALTERNATING_ROW_COLOR, DEFAULT_STY use super::popup::commands_help::CommandPopUp; use super::{filter::Filter, Model, StatefulTable}; -use crate::app::error::FlowrsError; use crate::app::worker::WorkerMessage; +use anyhow::Error; pub struct DagModel { pub all: Vec, @@ -27,7 +27,7 @@ pub struct DagModel { pub filtered: StatefulTable, pub filter: Filter, #[allow(dead_code)] - pub errors: Vec, + pub errors: Vec, commands: Option>, ticks: u32, event_buffer: Vec, @@ -130,7 +130,7 @@ impl Model for DagModel { } None => self .errors - .push(FlowrsError::from(String::from("No dag selected"))), + .push(Error::msg("No DAG selected to pause/resume")), }, KeyCode::Char('/') => { self.filter.toggle(); @@ -152,7 +152,7 @@ impl Model for DagModel { ); } else { self.errors - .push(FlowrsError::from(String::from("No dag selected"))); + .push(Error::msg("No DAG selected to view DAG Runs")); } } KeyCode::Char('g') => { diff --git a/src/app/model/logs.rs b/src/app/model/logs.rs index 9c9c6f9..edc8fd0 100644 --- a/src/app/model/logs.rs +++ b/src/app/model/logs.rs @@ -1,3 +1,4 @@ +use anyhow::Error; use crossterm::event::KeyCode; use ratatui::{ buffer::Buffer, @@ -13,7 +14,7 @@ use regex::Regex; use crate::{ airflow::model::log::Log, - app::{error::FlowrsError, events::custom::FlowrsEvent, worker::WorkerMessage}, + app::{events::custom::FlowrsEvent, worker::WorkerMessage}, ui::constants::DM_RGB, }; @@ -27,7 +28,7 @@ pub struct LogModel { pub all: Vec, pub current: usize, #[allow(dead_code)] - pub errors: Vec, + pub errors: Vec, ticks: u32, vertical_scroll: usize, vertical_scroll_state: ScrollbarState, diff --git a/src/app/model/taskinstances.rs b/src/app/model/taskinstances.rs index 0f4dd65..8626643 100644 --- a/src/app/model/taskinstances.rs +++ b/src/app/model/taskinstances.rs @@ -21,8 +21,8 @@ use super::popup::taskinstances::clear::ClearTaskInstancePopup; use super::popup::taskinstances::mark::MarkTaskInstancePopup; use super::popup::taskinstances::TaskInstancePopUp; use super::{filter::Filter, Model, StatefulTable}; -use crate::app::error::FlowrsError; use crate::app::worker::WorkerMessage; +use anyhow::Error; pub struct TaskInstanceModel { pub dag_id: Option, @@ -31,7 +31,7 @@ pub struct TaskInstanceModel { pub filtered: StatefulTable, pub filter: Filter, #[allow(dead_code)] - pub errors: Vec, + pub errors: Vec, pub popup: Option, pub marked: Vec, commands: Option>, diff --git a/src/app/state.rs b/src/app/state.rs index 0c5b2c1..95fe780 100644 --- a/src/app/state.rs +++ b/src/app/state.rs @@ -1,7 +1,7 @@ use crate::airflow::config::FlowrsConfig; -use crate::app::error::Result; use crate::app::model::dagruns::DagRunModel; use crate::app::model::dags::DagModel; +use anyhow::Result; use super::model::{config::ConfigModel, logs::LogModel, taskinstances::TaskInstanceModel}; diff --git a/src/commands/config/add.rs b/src/commands/config/add.rs index bd999f5..0367fcb 100644 --- a/src/commands/config/add.rs +++ b/src/commands/config/add.rs @@ -5,11 +5,11 @@ use log::info; use strum::IntoEnumIterator; use super::model::AddCommand; -use crate::app::error::Result; use crate::{ airflow::config::{AirflowAuth, AirflowConfig, BasicAuth, FlowrsConfig, TokenCmd}, commands::config::model::{validate_endpoint, ConfigOption}, }; +use anyhow::Result; impl AddCommand { pub fn run(&self) -> Result<()> { diff --git a/src/commands/config/list.rs b/src/commands/config/list.rs index 2003279..c515bd0 100644 --- a/src/commands/config/list.rs +++ b/src/commands/config/list.rs @@ -2,7 +2,7 @@ use std::path::Path; use super::model::ListCommand; use crate::airflow::config::FlowrsConfig; -use crate::app::error::Result; +use anyhow::Result; impl ListCommand { pub fn run(&self) -> Result<()> { diff --git a/src/commands/config/managed_services.rs b/src/commands/config/managed_services.rs index 7fe11a7..081fa1c 100644 --- a/src/commands/config/managed_services.rs +++ b/src/commands/config/managed_services.rs @@ -6,7 +6,7 @@ use strum::IntoEnumIterator; use super::model::ManagedServiceCommand; use crate::airflow::config::FlowrsConfig; use crate::airflow::config::ManagedService; -use crate::app::error::Result; +use anyhow::Result; impl ManagedServiceCommand { pub fn run(&self) -> Result<()> { diff --git a/src/commands/config/model.rs b/src/commands/config/model.rs index aaff4d1..5efa703 100644 --- a/src/commands/config/model.rs +++ b/src/commands/config/model.rs @@ -1,5 +1,5 @@ use crate::airflow::config::ManagedService; -use crate::app::error::Result; +use anyhow::Result; use clap::Parser; use inquire::validator::Validation; use strum::Display; diff --git a/src/commands/config/remove.rs b/src/commands/config/remove.rs index e6df470..4fb4e6b 100644 --- a/src/commands/config/remove.rs +++ b/src/commands/config/remove.rs @@ -4,7 +4,7 @@ use inquire::Select; use super::model::RemoveCommand; use crate::airflow::config::FlowrsConfig; -use crate::app::error::Result; +use anyhow::Result; impl RemoveCommand { pub fn run(&self) -> Result<()> { diff --git a/src/commands/config/update.rs b/src/commands/config/update.rs index 8408762..1539ece 100644 --- a/src/commands/config/update.rs +++ b/src/commands/config/update.rs @@ -10,7 +10,7 @@ use crate::{ commands::config::model::{validate_endpoint, ConfigOption}, }; -use crate::app::error::Result; +use anyhow::Result; impl UpdateCommand { pub fn run(&self) -> Result<()> { diff --git a/src/commands/run.rs b/src/commands/run.rs index 8f810cf..9f9ba10 100644 --- a/src/commands/run.rs +++ b/src/commands/run.rs @@ -17,7 +17,8 @@ use simplelog::{Config, WriteLogger}; use crate::airflow::config::FlowrsConfig; use crate::app::run_app; -use crate::app::{error::Result, state::App}; +use crate::app::state::App; +use anyhow::Result; #[derive(Parser, Debug)] pub struct RunCommand { diff --git a/src/main.rs b/src/main.rs index be77093..aeb2821 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ lazy_static::lazy_static! { pub static ref CONFIG_FILE: PathBuf = home_dir().unwrap().join(".flowrs"); } -use app::error::Result; +use anyhow::Result; #[derive(Parser)] #[clap(name="flowrs", bin_name="flowrs", version, about, before_help=ASCII_LOGO)]