From 6e5b91b5fe6e9512fc70b84b9cfd5af1867340a5 Mon Sep 17 00:00:00 2001 From: Max Dymond Date: Fri, 7 Jul 2023 16:45:43 +0100 Subject: [PATCH] Combine errors into a single FlokiError enum. Signed-off-by: Max Dymond --- CHANGELOG.md | 1 + src/config.rs | 33 +++++++++++++++++---------------- src/environment.rs | 14 ++++++-------- src/errors.rs | 23 ++++++++--------------- src/main.rs | 7 ++----- 5 files changed, 34 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b672afb..3647846 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ Status: Available for use can use `toml(file="")` in order to load values. - Add `floki render` to print out the rendered configuration template. - Switch rust image to `rust:1-alpine3.18` as it's better maintained. +- Combine errors into a single FlokiError enum. ### Fixed diff --git a/src/config.rs b/src/config.rs index 4f242fc..c6799d0 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,7 +1,6 @@ /// Configuration file format for floki -use crate::errors; +use crate::errors::FlokiError; use crate::image; -use anyhow::Error; use serde::{Deserialize, Serialize}; use tera::from_value; use tera::Context; @@ -126,7 +125,7 @@ fn tomlloader(args: &HashMap) -> tera::Result } // Renders a template from a given string. -pub fn render_template(template: &str, source_filename: &Path) -> Result { +pub fn render_template(template: &str, source_filename: &Path) -> Result { let template_path = source_filename.display().to_string(); debug!("Rendering template: {template_path}"); @@ -140,7 +139,7 @@ pub fn render_template(template: &str, source_filename: &Path) -> Result Result Result { - let content = std::fs::read_to_string(file).map_err(|e| { - errors::FlokiError::ProblemOpeningConfigYaml { + pub fn render(file: &Path) -> Result { + let content = + std::fs::read_to_string(file).map_err(|e| FlokiError::ProblemOpeningConfigYaml { name: file.display().to_string(), error: e, - } - })?; + })?; // Render the template first before parsing it. render_template(&content, file) } - pub fn from_file(file: &Path) -> Result { + pub fn from_file(file: &Path) -> Result { debug!("Reading configuration file: {:?}", file); // Render the output from the configuration file before parsing. let output = Self::render(file)?; // Parse the rendered floki file from the string. - let mut config: FlokiConfig = serde_yaml::from_str(&output).map_err(|e| { - errors::FlokiError::ProblemParsingConfigYaml { + let mut config: FlokiConfig = + serde_yaml::from_str(&output).map_err(|e| FlokiError::ProblemParsingConfigYaml { name: file.display().to_string(), error: e, - } - })?; + })?; // Ensure the path to an external yaml file is correct. // If the image.yaml.path file is relative, then it should @@ -191,7 +192,7 @@ impl FlokiConfig { if yaml.file.is_relative() { yaml.file = file .parent() - .ok_or_else(|| errors::FlokiInternalError::InternalAssertionFailed { + .ok_or_else(|| FlokiError::InternalAssertionFailed { description: format!( "could not construct path to external yaml file '{:?}'", &yaml.file diff --git a/src/environment.rs b/src/environment.rs index 2782ae6..1ea2250 100644 --- a/src/environment.rs +++ b/src/environment.rs @@ -1,5 +1,5 @@ /// Query the current user environment -use crate::errors; +use crate::errors::FlokiError; use anyhow::Error; use std::env; use std::ffi::OsString; @@ -79,14 +79,14 @@ fn find_floki_yaml(current_directory: &path::Path) -> Result Result<(path::PathBuf, path::PathBuf), Error> { let dir = path .parent() - .ok_or_else(|| errors::FlokiInternalError::InternalAssertionFailed { + .ok_or_else(|| FlokiError::InternalAssertionFailed { description: format!("config_file '{:?}' does not have a parent", &path), })? .to_path_buf(); @@ -116,11 +116,9 @@ fn get_floki_work_path(uid: nix::unistd::Uid) -> path::PathBuf { /// Normalize the filepath - this turns a relative path into an absolute one - to /// do this it must locate the file in the filesystem, and hence it may fail. fn normalize_path(path: path::PathBuf) -> Result { - let res = std::fs::canonicalize(&path).map_err(|e| { - errors::FlokiError::ProblemNormalizingFilePath { - name: path.display().to_string(), - error: e, - } + let res = std::fs::canonicalize(&path).map_err(|e| FlokiError::ProblemNormalizingFilePath { + name: path.display().to_string(), + error: e, })?; Ok(res) diff --git a/src/errors.rs b/src/errors.rs index 57cd71f..3cadef6 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -65,6 +65,14 @@ pub enum FlokiError { #[error("Malformed item in docker_switches: {item}")] MalformedDockerSwitch { item: String }, + + /// Internal error for floki - these represent failed assumptions of + /// the developers, and shouldn't actually manifest. + #[error("An internal assertion failed '{description}'. This is probably a bug!")] + InternalAssertionFailed { description: String }, + + #[error("Invalid verbosity setting of {setting:?}. Use a setting between 0 and 3 (-vvv)")] + InvalidVerbositySetting { setting: u8 }, } /// Generate a summary string for a process exiting @@ -98,18 +106,3 @@ impl fmt::Display for FlokiSubprocessExitStatus { ) } } - -/// Internal error types for floki - these represent failed assumptions of -/// the developers, and shouldn't actually manifest. -#[derive(Debug, thiserror::Error)] -pub enum FlokiInternalError { - #[error("An internal assertion failed '{description}'. This is probably a bug!")] - InternalAssertionFailed { description: String }, -} - -/// Errors made by floki users. -#[derive(Debug, thiserror::Error)] -pub enum FlokiUserError { - #[error("Invalid verbosity setting of {setting:?}. Use a setting between 0 and 3 (-vvv)")] - InvalidVerbositySetting { setting: u8 }, -} diff --git a/src/main.rs b/src/main.rs index 20f4b57..298e81f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,7 @@ use anyhow::Error; use cli::{Cli, Subcommand}; use config::FlokiConfig; use environment::Environment; +use errors::FlokiError; use structopt::StructOpt; fn main() -> Result<(), Error> { @@ -87,11 +88,7 @@ fn configure_logging(verbosity: u8) -> Result<(), Error> { 1 => log::LevelFilter::Info, 2 => log::LevelFilter::Debug, 3 => log::LevelFilter::Trace, - _ => { - return Err( - errors::FlokiUserError::InvalidVerbositySetting { setting: verbosity }.into(), - ) - } + _ => return Err(FlokiError::InvalidVerbositySetting { setting: verbosity }.into()), }; simplelog::TermLogger::init( level,