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

feat: add a user-friendly message for AddrInUse error #233

Merged
merged 2 commits into from
Mar 7, 2025
Merged
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
72 changes: 71 additions & 1 deletion cot/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,42 @@ impl CliTask for RunServer {
};

let bootstrapper = bootstrapper.boot().await?;
crate::run(bootstrapper, &addr_port).await

let result = crate::run(bootstrapper, &addr_port).await;
if let Err(error) = &result {
if let Some(user_friendly_error) = Self::get_user_friendly_error(error, &addr_port) {
eprintln!("{user_friendly_error}");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we handled a specific error, shouldn't we exit early here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think (I might be wrong) that it still makes sense to print out the entire error structure anyways for debugging purposes. Also, this gives a chance to cleanup the stack with RAII as opposed to std::process::exit which terminates instantly.

}
}

result
}
}

impl RunServer {
fn get_user_friendly_error(error: &Error, addr_port: &str) -> Option<String> {
match &error.inner {
ErrorRepr::StartServer { source } => match source.kind() {
std::io::ErrorKind::AddrInUse => {
let exec = std::env::args()
.next()
.unwrap_or_else(|| "<server binary>".to_owned());

Some(format!(
"The address you are trying to start the server at ({addr_port}) is \
already in use by a different program. You might want to use the \
-l/--listen option to specify a different port to run the server at. \
For example, to run the server at port 8888:\n\
\n\
{exec} -l 8888\n\
cargo run -- -l 8888\n\
bacon serve -- -- -l 8888"
))
}
_ => None,
},
_ => None,
}
}
}

Expand Down Expand Up @@ -522,4 +557,39 @@ mod tests {
let bootstrapper = Bootstrapper::new(TestProject).with_config_name("test")?;
check.execute(&matches, bootstrapper).await
}

#[test]
fn get_user_friendly_error_addr_in_use() {
let source = std::io::Error::new(std::io::ErrorKind::AddrInUse, "error");
let error = Error::new(ErrorRepr::StartServer { source });

let message = RunServer::get_user_friendly_error(&error, "1.2.3.4:8123");

assert!(message.is_some());
let message = message.unwrap();
assert!(message.contains("1.2.3.4:8123"));
assert!(message.contains("is already in use"));
}

#[test]
fn get_user_friendly_error_io_error_other() {
let source = std::io::Error::new(std::io::ErrorKind::Other, "error");
let error = Error::new(ErrorRepr::StartServer { source });

let message = RunServer::get_user_friendly_error(&error, "1.2.3.4:8123");

assert!(message.is_none());
}

#[test]
fn get_user_friendly_error_unsupported_error() {
let error = Error::new(ErrorRepr::NoViewToReverse {
app_name: None,
view_name: "test".to_string(),
});

let message = RunServer::get_user_friendly_error(&error, "1.2.3.4:8123");

assert!(message.is_none());
}
}