summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJokler <jokler.contact@gmail.com>2017-10-17 05:14:40 +0200
committerJokler <jokler.contact@gmail.com>2017-10-17 05:14:40 +0200
commit44cec3059fcc5697f7347260eb77a952cfca18ab (patch)
tree504562c5b47d1fd3b47acd3a9b7a6c7ab66d0be4
parent8b64bdc7a96a1b5f4549493fb30042e1a46349a6 (diff)
downloadfrippy-44cec3059fcc5697f7347260eb77a952cfca18ab.tar.gz
frippy-44cec3059fcc5697f7347260eb77a952cfca18ab.zip
Remove a few unwraps and make error handling more concise
-rw-r--r--src/lib.rs43
-rw-r--r--src/plugins/currency.rs7
2 files changed, 21 insertions, 29 deletions
diff --git a/src/lib.rs b/src/lib.rs
index cc9f016..da36d91 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -64,7 +64,7 @@ pub fn run() {
Ok(v) => configs.push(v),
Err(e) => error!("Incorrect config file {}", e),
}
- },
+ }
Err(e) => error!("Failed to read path {}", e),
}
}
@@ -94,24 +94,16 @@ pub fn run() {
// Open a connection and add work for each config
for config in configs {
- let fut = match IrcServer::new_future(reactor.handle(), &config) {
- Ok(v) => v,
- Err(e) => {
- error!("Failed to connect: {}", e);
- return;
- }
- };
+ let server =
+ match IrcServer::new_future(reactor.handle(), &config).and_then(|f| reactor.run(f)) {
+ Ok(v) => v,
+ Err(e) => {
+ error!("Failed to connect: {}", e);
+ return;
+ }
+ };
- let server = match reactor.run(fut) {
- Ok(v) => {
- info!("Connected to server");
- v
- }
- Err(e) => {
- error!("Failed to connect: {}", e);
- return;
- }
- };
+ info!("Connected to server");
match server.identify() {
Ok(_) => info!("Identified"),
@@ -125,7 +117,7 @@ pub fn run() {
let task = server
.stream()
.for_each(move |message| process_msg(&server, &plugin_names, plugins.clone(), message))
- .map_err(|e| Err(e).unwrap());
+ .map_err(|e| error!("Failed to process message: {}", e));
reactor.handle().spawn(task);
}
@@ -154,10 +146,10 @@ fn process_msg(server: &IrcServer,
if let Some(ref c) = command {
if c.tokens.is_empty() {
let help = format!("Use \"{} help\" to get help", server.current_nickname());
- server.send_notice(&c.source, &help).unwrap();
+ server.send_notice(&c.source, &help)?;
} else if "help" == &c.tokens[0].to_lowercase() {
- send_help_message(server, c).unwrap();
+ send_help_message(server, c)?;
} else if !plugin_names.contains(&c.tokens[0].to_lowercase()) {
@@ -166,7 +158,7 @@ fn process_msg(server: &IrcServer,
server.current_nickname(),
c.tokens[0]);
- server.send_notice(&c.source, &help).unwrap();
+ server.send_notice(&c.source, &help)?;
}
}
@@ -181,7 +173,12 @@ fn process_msg(server: &IrcServer,
let server = server.clone();
// Execute the plugin in another thread
- spawn(move || { lock_plugin!(plugin).execute(&server, &message).unwrap(); });
+ spawn(move || {
+ if let Err(e) = lock_plugin!(plugin).execute(&server, &message) {
+ let name = lock_plugin!(plugin).name().to_string();
+ error!("Error in {} - {}", name, e);
+ };
+ });
}
// Check if the command is for this plugin
diff --git a/src/plugins/currency.rs b/src/plugins/currency.rs
index bc87592..0db8240 100644
--- a/src/plugins/currency.rs
+++ b/src/plugins/currency.rs
@@ -67,12 +67,7 @@ impl Currency {
}
fn eval_command<'a>(&self, tokens: &'a [String]) -> Option<ConvertionRequest<'a>> {
- let parsed = match tokens[0].parse() {
- Ok(v) => v,
- Err(_) => {
- return None;
- }
- };
+ let parsed = tokens[0].parse().ok()?;
Some(ConvertionRequest {
value: parsed,