Skip to content

Commit 64664e8

Browse files
authored
Merge pull request #547 from shaneikennedy/command-error-handling
2 parents b902b17 + dbf81f7 commit 64664e8

File tree

5 files changed

+50
-47
lines changed

5 files changed

+50
-47
lines changed

Cargo.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tansu-cli/src/cli.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -122,26 +122,10 @@ impl Cli {
122122
let cli = Cli::parse();
123123

124124
match cli.command.unwrap_or(Command::Broker(Box::new(cli.broker))) {
125-
Command::Broker(arg) => arg
126-
.main()
127-
.await
128-
.inspect(|result| debug!(?result))
129-
.inspect_err(|err| debug!(?err)),
130-
125+
Command::Broker(arg) => arg.main().await,
131126
Command::Cat { command } => command.main().await,
132-
133-
Command::Generator(arg) => arg
134-
.main()
135-
.await
136-
.inspect(|result| debug!(?result))
137-
.inspect_err(|err| debug!(?err)),
138-
139-
Command::Perf(arg) => arg
140-
.main()
141-
.await
142-
.inspect(|result| debug!(?result))
143-
.inspect_err(|err| debug!(?err)),
144-
127+
Command::Generator(arg) => arg.main().await,
128+
Command::Perf(arg) => arg.main().await,
145129
Command::Proxy(arg) => tansu_proxy::Proxy::main(
146130
arg.listener_url.into_inner(),
147131
arg.advertised_listener_url.into_inner(),
@@ -151,7 +135,6 @@ impl Cli {
151135
)
152136
.await
153137
.map_err(Into::into),
154-
155138
Command::Topic { command } => command.main().await,
156139
}
157140
}

tansu-cli/src/cli/topic.rs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use crate::Result;
1818
use clap::Subcommand;
1919
use tansu_sans_io::ErrorCode;
2020
use tansu_topic::Topic;
21-
use tracing::{debug, error, info};
2221
use url::Url;
2322

2423
use super::DEFAULT_BROKER;
@@ -84,32 +83,9 @@ impl From<Command> for Topic {
8483
}
8584
}
8685

87-
const CLIENT_ERROR_MESSAGE: &str = "A client error occurred. Possible causes:
88-
• No network connection
89-
• The server is down or unreachable
90-
• Incorrect hostname or port
91-
• Firewall or proxy blocking the connection
92-
• TLS/SSL certificate issues (if applicable)
93-
94-
Check your internet connection, verify the server address, and try again.";
95-
9686
impl Command {
9787
pub(super) async fn main(self) -> Result<ErrorCode> {
98-
Topic::from(self)
99-
.main()
100-
.await
101-
.map_err(Into::into)
102-
.inspect(|res| match res {
103-
ErrorCode::None => info!("Topic command exited successfully."),
104-
_ => error!("{}", res),
105-
})
106-
.inspect_err(|e| match e {
107-
crate::Error::Topic(error) => match error {
108-
tansu_topic::Error::Client(_) => error!("{}", CLIENT_ERROR_MESSAGE),
109-
_ => error!("Unknown error occurred during command: {}", error),
110-
},
111-
_ => debug!(?e),
112-
})
88+
Topic::from(self).main().await.map_err(Into::into)
11389
}
11490
}
11591

tansu/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@ workspace = true
1616
[dependencies]
1717
dotenv.workspace = true
1818
tansu-broker = { workspace = true, default-features = false, optional = true }
19+
tansu-cat.workspace = true
1920
tansu-cli = { workspace = true, default-features = false, optional = true }
21+
tansu-generator.workspace = true
22+
tansu-perf.workspace = true
23+
tansu-proxy.workspace = true
2024
tansu-sans-io.workspace = true
2125
tansu-schema = { workspace = true, default-features = false, optional = true }
2226
tansu-storage = { workspace = true, default-features = false, optional = true }
27+
tansu-topic.workspace = true
2328
tokio.workspace = true
2429
tracing.workspace = true
2530

tansu/src/bin/tansu.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ use tansu_cli::{Cli, Result};
1818
use tansu_sans_io::ErrorCode;
1919
use tracing::{debug, error};
2020

21+
const CLIENT_ERROR_MESSAGE: &str = "A client error occurred. Possible causes:
22+
• No network connection
23+
• The server is down or unreachable
24+
• Incorrect hostname or port
25+
• Firewall or proxy blocking the connection
26+
• TLS/SSL certificate issues (if applicable)
27+
28+
Check your internet connection, verify the server address, and try again.";
29+
2130
#[tokio::main]
2231
async fn main() -> Result<ErrorCode> {
2332
_ = dotenv().ok();
@@ -26,6 +35,31 @@ async fn main() -> Result<ErrorCode> {
2635

2736
Cli::main()
2837
.await
29-
.inspect(|error_code| debug!(%error_code))
30-
.inspect_err(|err| error!(%err))
38+
.inspect(|error_code| match error_code {
39+
ErrorCode::None => debug!("{}", error_code),
40+
_ => error!("{}", error_code),
41+
})
42+
.inspect_err(|err| match err {
43+
tansu_cli::Error::Cat(error) => match &**error {
44+
tansu_cat::Error::Client(_) => error!("{}", CLIENT_ERROR_MESSAGE),
45+
_ => error!("Unknown error occurred during command: {}", error),
46+
},
47+
tansu_cli::Error::Generate(error) => match error {
48+
tansu_generator::Error::Client(_) => error!("{}", CLIENT_ERROR_MESSAGE),
49+
_ => error!("Unknown error occurred during command: {}", error),
50+
},
51+
tansu_cli::Error::Perf(error) => match error {
52+
tansu_perf::Error::Client(_) => error!("{}", CLIENT_ERROR_MESSAGE),
53+
_ => error!("Unknown error occurred during command: {}", error),
54+
},
55+
tansu_cli::Error::Proxy(error) => match error {
56+
tansu_proxy::Error::Client(_) => error!("{}", CLIENT_ERROR_MESSAGE),
57+
_ => error!("Unknown error occurred during command: {}", error),
58+
},
59+
tansu_cli::Error::Topic(error) => match error {
60+
tansu_topic::Error::Client(_) => error!("{}", CLIENT_ERROR_MESSAGE),
61+
_ => error!("Unknown error occurred during command: {}", error),
62+
},
63+
_ => error!("Unknown error occurred during command: {}", err),
64+
})
3165
}

0 commit comments

Comments
 (0)