Skip to content

Commit

Permalink
Better error reporting for config and other params (#957)
Browse files Browse the repository at this point in the history
This prints an extended error message, explaining to the user what
params must be removed.

Fixes #938
  • Loading branch information
nyurik authored Oct 22, 2023
1 parent 2053f80 commit 2e7a179
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
4 changes: 2 additions & 2 deletions martin/src/args/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Args {
warn!("The WATCH_MODE env variable is no longer supported, and will be ignored");
}
if self.meta.config.is_some() && !self.meta.connection.is_empty() {
return Err(Error::ConfigAndConnectionsError);
return Err(Error::ConfigAndConnectionsError(self.meta.connection));
}

self.srv.merge_into_config(&mut config.srv);
Expand Down Expand Up @@ -174,7 +174,7 @@ mod tests {
let env = FauxEnv::default();
let mut config = Config::default();
let err = args.merge_into_config(&mut config, &env).unwrap_err();
assert!(matches!(err, crate::Error::ConfigAndConnectionsError));
assert!(matches!(err, crate::Error::ConfigAndConnectionsError(..)));
}

#[test]
Expand Down
25 changes: 23 additions & 2 deletions martin/src/utils/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fmt::Write;
use std::io;
use std::path::PathBuf;

Expand All @@ -7,10 +8,30 @@ use crate::sprites::SpriteError;

pub type Result<T> = std::result::Result<T, Error>;

fn elide_vec(vec: &[String], max_items: usize, max_len: usize) -> String {
let mut s = String::new();
for (i, v) in vec.iter().enumerate() {
if i > max_items {
let _ = write!(s, " and {} more", vec.len() - i);
break;
}
if i > 0 {
s.push(' ');
}
if v.len() > max_len {
s.push_str(&v[..max_len]);
s.push('…');
} else {
s.push_str(v);
}
}
s
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("The --config and the connection parameters cannot be used together")]
ConfigAndConnectionsError,
#[error("The --config and the connection parameters cannot be used together. Please remove unsupported parameters '{}'", elide_vec(.0, 3, 15))]
ConfigAndConnectionsError(Vec<String>),

#[error("Unable to bind to {1}: {0}")]
BindingError(io::Error, String),
Expand Down

0 comments on commit 2e7a179

Please sign in to comment.