Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error reporting for config and other params #957

Merged
merged 1 commit into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading