Improve error handling for events and in general

This commit is contained in:
Tobias Reisinger 2024-03-27 15:37:52 +01:00
parent 8d1e813c0b
commit f860fe3689
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
6 changed files with 114 additions and 104 deletions

View file

@ -1,11 +1,10 @@
use std::process::exit;
use std::io::{Error, ErrorKind};
use telnet::Telnet;
use crate::cli::Commands;
use crate::models::{Channel, Event};
use crate::models::Channel;
use crate::models::Client;
use crate::response::Response;
mod wrappers;
mod commands;
@ -14,41 +13,42 @@ mod cli;
mod utils;
mod models;
mod response;
mod command_utils;
fn channel_or_exit(channel_res: Result<Option<Channel>, String>) -> Channel {
channel_res.unwrap_or_else(|err| {
println!("Failed to find channel: {}", err);
exit(1);
})
.unwrap_or_else(|| {
println!("Failed to find channel.");
exit(1);
})
fn channel_or_error(channel_res: Result<Option<Channel>, String>) -> Result<Channel, Error> {
channel_res.map_err(|err| make_action_error("find channel", err))?
.ok_or_else(|| make_action_error("find channel", String::from("Not Found.")))
}
fn client_or_exit(client_res: Result<Option<Client>, String>) -> Client {
client_res.unwrap_or_else(|err| {
println!("Failed to find client: {}", err);
exit(1);
})
.unwrap_or_else(|| {
println!("Failed to find client.");
exit(1);
})
fn client_or_error(client_res: Result<Option<Client>, String>) -> Result<Client, Error> {
client_res.map_err(|err| make_action_error("find client", err))?
.ok_or_else(|| make_action_error("find client", String::from("Not Found.")))
}
fn main() {
fn connect() -> Result<Telnet, Error> {
let mut connection = Telnet::connect(("127.0.0.1", 25639), 512 * 1024)
.map_err(|_| Error::new(ErrorKind::AddrNotAvailable, String::from("Failed to connect to Teamspeak.")))?;
wrappers::skip_welcome(&mut connection)
.map_err(to_other_error)?;
wrappers::login(&mut connection)
.map_err(|msg| Error::new(ErrorKind::PermissionDenied, msg))?;
Ok(connection)
}
fn to_other_error(msg: String) -> Error {
Error::new(ErrorKind::Other, msg)
}
fn make_action_error(action: &str, msg: String) -> Error {
Error::new(ErrorKind::Other, format!("Failed to {}: {}", action, msg))
}
fn main() -> Result<(), Error> {
let cli = cli::init();
let connection = Telnet::connect(("127.0.0.1", 25639), 512 * 1024);
if connection.is_err() {
println!("Failed to connect to Teamspeak.");
exit(1);
}
let mut connection = connection.unwrap();
wrappers::skip_welcome(&mut connection);
wrappers::login(&mut connection);
let mut connection = connect()?;
// You can check for the existence of subcommands, and if found use their
// matches just as you would the top level cmd
@ -61,8 +61,7 @@ fn main() {
}
}
Err(msg) => {
println!("Failed to get channels: {}", msg);
exit(1);
return Err(make_action_error("get channels", msg));
}
}
}
@ -75,71 +74,62 @@ fn main() {
}
}
Err(msg) => {
println!("Failed to get clients: {}", msg);
exit(1);
return Err(make_action_error("get clients", msg));
}
}
}
Commands::Fetch(args) => {
if args.want_client() && args.want_channel() {
println!("Fetching both clients and channels is not supported.");
exit(1);
return Err(Error::new(ErrorKind::InvalidInput, "Fetching both clients and channels is not supported."));
}
if !args.want_client() && !args.want_channel() {
println!("No clients or channels specified.");
exit(1);
return Err(Error::new(ErrorKind::InvalidInput, "No clients or channels specified."));
}
if args.want_client() {
let client = client_or_exit(args.client(&mut connection));
let client = client_or_error(args.client(&mut connection))?;
match wrappers::fetch_client(&mut connection, &[client]) {
Ok(_) => println!("Successfully fetched client."),
Err(msg) => {
println!("Failed to fetch client: {}", msg);
exit(1);
return Err(make_action_error("fetch client", msg));
}
}
}
if args.want_channel() {
let channel = channel_or_exit(args.channel(&mut connection));
let channel = channel_or_error(args.channel(&mut connection))?;
match wrappers::fetch_channel(&mut connection, channel) {
Ok(_) => println!("Successfully fetched channel."),
Err(msg) => {
println!("Failed to fetch channel: {}", msg);
exit(1);
return Err(make_action_error("fetch channel", msg));
}
}
}
}
Commands::Message(args) => {
let target = args.target(&mut connection).unwrap_or_else(|err| {
println!("Failed to get message target: {}", err);
exit(1);
});
let target = args.target(&mut connection)
.map_err(|msg| make_action_error("message target", msg))?;
match wrappers::send_text_message(&mut connection, target, args.message) {
Ok(_) => println!("Successfully sent message."),
Err(msg) => {
println!("Failed to send message: {}", msg);
exit(1);
return Err(make_action_error("send message", msg));
}
}
}
Commands::Move(args) => {
let channel = channel_or_exit(args.channel(&mut connection));
let client = client_or_exit(args.client(&mut connection));
let channel = channel_or_error(args.channel(&mut connection))?;
let client = client_or_error(args.client(&mut connection))?;
match wrappers::move_client(&mut connection, &channel, &[client]) {
Ok(resp) => println!("Successfully moved client: {}", resp),
Err(msg) => {
println!("Failed to move client: {}", msg);
exit(1);
return Err(make_action_error("move client", msg));
}
}
}
@ -148,40 +138,22 @@ fn main() {
match wrappers::update_client(&mut connection, args.to_parameter_list()) {
Ok(_) => println!("Successfully updated client."),
Err(msg) => {
println!("Failed to update client: {}", msg);
exit(1);
return Err(make_action_error("update client", msg));
}
}
}
Commands::Events(args) => {
for event in args.event {
if commands::clientnotifyregister(&mut connection, 1, event).is_err() {
println!("Failed to register event listener.");
exit(1);
}
}
loop {
match commands::read_response(&mut connection, true, String::new()) {
Ok(response) => {
if let Response::Event(event_type, params) = response {
command_utils::events::register_events(&mut connection, args.event.clone())
.map_err(to_other_error)?;
command_utils::events::loop_response_reader(&mut connection);
let event = Event::new(&mut connection, event_type, params);
match serde_json::to_string(&event) {
Ok(json) => println!("{}", json),
Err(_) => {
// TODO: Handle error
}
}
}
}
Err(_) => {
// TODO: Handle error
}
}
// loop_response_reader failed. Let's try to reconnect after 1 second.
std::thread::sleep(std::time::Duration::from_secs(1));
connection = connect()?;
}
}
}
Ok(())
}