Skip to content

Commit 3cc2884

Browse files
author
Max Dymond
authored
Merge pull request #300 from Metaswitch/md/uniteerrors
Combine errors into a single FlokiError enum.
2 parents 6dfe95c + 6e5b91b commit 3cc2884

5 files changed

Lines changed: 34 additions & 44 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Status: Available for use
2222
can use `toml(file="<filepath>")` in order to load values.
2323
- Add `floki render` to print out the rendered configuration template.
2424
- Switch rust image to `rust:1-alpine3.18` as it's better maintained.
25+
- Combine errors into a single FlokiError enum.
2526

2627
### Fixed
2728

src/config.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/// Configuration file format for floki
2-
use crate::errors;
2+
use crate::errors::FlokiError;
33
use crate::image;
4-
use anyhow::Error;
54
use serde::{Deserialize, Serialize};
65
use tera::from_value;
76
use tera::Context;
@@ -126,7 +125,7 @@ fn tomlloader(args: &HashMap<String, tera::Value>) -> tera::Result<tera::Value>
126125
}
127126

128127
// Renders a template from a given string.
129-
pub fn render_template(template: &str, source_filename: &Path) -> Result<String, Error> {
128+
pub fn render_template(template: &str, source_filename: &Path) -> Result<String, FlokiError> {
130129
let template_path = source_filename.display().to_string();
131130

132131
debug!("Rendering template: {template_path}");
@@ -140,7 +139,7 @@ pub fn render_template(template: &str, source_filename: &Path) -> Result<String,
140139
tera.register_function("toml", tomlloader);
141140

142141
tera.add_raw_template(&template_path, template)
143-
.map_err(|e| errors::FlokiError::ProblemRenderingTemplate {
142+
.map_err(|e| FlokiError::ProblemRenderingTemplate {
144143
name: template_path.clone(),
145144
error: e,
146145
})?;
@@ -152,35 +151,37 @@ pub fn render_template(template: &str, source_filename: &Path) -> Result<String,
152151
context.insert("env", &vars);
153152

154153
// Render the floki file to string using the context.
155-
Ok(tera.render(&template_path, &context)?)
154+
tera.render(&template_path, &context)
155+
.map_err(|e| FlokiError::ProblemRenderingTemplate {
156+
name: template_path.clone(),
157+
error: e,
158+
})
156159
}
157160

158161
impl FlokiConfig {
159-
pub fn render(file: &Path) -> Result<String, Error> {
160-
let content = std::fs::read_to_string(file).map_err(|e| {
161-
errors::FlokiError::ProblemOpeningConfigYaml {
162+
pub fn render(file: &Path) -> Result<String, FlokiError> {
163+
let content =
164+
std::fs::read_to_string(file).map_err(|e| FlokiError::ProblemOpeningConfigYaml {
162165
name: file.display().to_string(),
163166
error: e,
164-
}
165-
})?;
167+
})?;
166168

167169
// Render the template first before parsing it.
168170
render_template(&content, file)
169171
}
170172

171-
pub fn from_file(file: &Path) -> Result<Self, Error> {
173+
pub fn from_file(file: &Path) -> Result<Self, FlokiError> {
172174
debug!("Reading configuration file: {:?}", file);
173175

174176
// Render the output from the configuration file before parsing.
175177
let output = Self::render(file)?;
176178

177179
// Parse the rendered floki file from the string.
178-
let mut config: FlokiConfig = serde_yaml::from_str(&output).map_err(|e| {
179-
errors::FlokiError::ProblemParsingConfigYaml {
180+
let mut config: FlokiConfig =
181+
serde_yaml::from_str(&output).map_err(|e| FlokiError::ProblemParsingConfigYaml {
180182
name: file.display().to_string(),
181183
error: e,
182-
}
183-
})?;
184+
})?;
184185

185186
// Ensure the path to an external yaml file is correct.
186187
// If the image.yaml.path file is relative, then it should
@@ -191,7 +192,7 @@ impl FlokiConfig {
191192
if yaml.file.is_relative() {
192193
yaml.file = file
193194
.parent()
194-
.ok_or_else(|| errors::FlokiInternalError::InternalAssertionFailed {
195+
.ok_or_else(|| FlokiError::InternalAssertionFailed {
195196
description: format!(
196197
"could not construct path to external yaml file '{:?}'",
197198
&yaml.file

src/environment.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// Query the current user environment
2-
use crate::errors;
2+
use crate::errors::FlokiError;
33
use anyhow::Error;
44
use std::env;
55
use std::ffi::OsString;
@@ -79,14 +79,14 @@ fn find_floki_yaml(current_directory: &path::Path) -> Result<path::PathBuf, Erro
7979
.ancestors()
8080
.map(|a| a.join("floki.yaml"))
8181
.find(|f| f.is_file())
82-
.ok_or_else(|| errors::FlokiError::ProblemFindingConfigYaml {}.into())
82+
.ok_or_else(|| FlokiError::ProblemFindingConfigYaml {}.into())
8383
}
8484

8585
/// Take a file path, and return a tuple consisting of its parent directory and the file path
8686
fn locate_file_in_parents(path: path::PathBuf) -> Result<(path::PathBuf, path::PathBuf), Error> {
8787
let dir = path
8888
.parent()
89-
.ok_or_else(|| errors::FlokiInternalError::InternalAssertionFailed {
89+
.ok_or_else(|| FlokiError::InternalAssertionFailed {
9090
description: format!("config_file '{:?}' does not have a parent", &path),
9191
})?
9292
.to_path_buf();
@@ -116,11 +116,9 @@ fn get_floki_work_path(uid: nix::unistd::Uid) -> path::PathBuf {
116116
/// Normalize the filepath - this turns a relative path into an absolute one - to
117117
/// do this it must locate the file in the filesystem, and hence it may fail.
118118
fn normalize_path(path: path::PathBuf) -> Result<path::PathBuf, Error> {
119-
let res = std::fs::canonicalize(&path).map_err(|e| {
120-
errors::FlokiError::ProblemNormalizingFilePath {
121-
name: path.display().to_string(),
122-
error: e,
123-
}
119+
let res = std::fs::canonicalize(&path).map_err(|e| FlokiError::ProblemNormalizingFilePath {
120+
name: path.display().to_string(),
121+
error: e,
124122
})?;
125123

126124
Ok(res)

src/errors.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ pub enum FlokiError {
6565

6666
#[error("Malformed item in docker_switches: {item}")]
6767
MalformedDockerSwitch { item: String },
68+
69+
/// Internal error for floki - these represent failed assumptions of
70+
/// the developers, and shouldn't actually manifest.
71+
#[error("An internal assertion failed '{description}'. This is probably a bug!")]
72+
InternalAssertionFailed { description: String },
73+
74+
#[error("Invalid verbosity setting of {setting:?}. Use a setting between 0 and 3 (-vvv)")]
75+
InvalidVerbositySetting { setting: u8 },
6876
}
6977

7078
/// Generate a summary string for a process exiting
@@ -98,18 +106,3 @@ impl fmt::Display for FlokiSubprocessExitStatus {
98106
)
99107
}
100108
}
101-
102-
/// Internal error types for floki - these represent failed assumptions of
103-
/// the developers, and shouldn't actually manifest.
104-
#[derive(Debug, thiserror::Error)]
105-
pub enum FlokiInternalError {
106-
#[error("An internal assertion failed '{description}'. This is probably a bug!")]
107-
InternalAssertionFailed { description: String },
108-
}
109-
110-
/// Errors made by floki users.
111-
#[derive(Debug, thiserror::Error)]
112-
pub enum FlokiUserError {
113-
#[error("Invalid verbosity setting of {setting:?}. Use a setting between 0 and 3 (-vvv)")]
114-
InvalidVerbositySetting { setting: u8 },
115-
}

src/main.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use anyhow::Error;
1717
use cli::{Cli, Subcommand};
1818
use config::FlokiConfig;
1919
use environment::Environment;
20+
use errors::FlokiError;
2021
use structopt::StructOpt;
2122

2223
fn main() -> Result<(), Error> {
@@ -87,11 +88,7 @@ fn configure_logging(verbosity: u8) -> Result<(), Error> {
8788
1 => log::LevelFilter::Info,
8889
2 => log::LevelFilter::Debug,
8990
3 => log::LevelFilter::Trace,
90-
_ => {
91-
return Err(
92-
errors::FlokiUserError::InvalidVerbositySetting { setting: verbosity }.into(),
93-
)
94-
}
91+
_ => return Err(FlokiError::InvalidVerbositySetting { setting: verbosity }.into()),
9592
};
9693
simplelog::TermLogger::init(
9794
level,

0 commit comments

Comments
 (0)