diff options
| author | Jokler <jokler.contact@gmail.com> | 2018-05-12 00:21:59 +0200 |
|---|---|---|
| committer | Jokler <jokler.contact@gmail.com> | 2018-05-12 00:21:59 +0200 |
| commit | 6ec0c444f642630485ca18b3043191b67a6f8e8c (patch) | |
| tree | 433d04f5b51dfb287536dd624b5c24075b5d227f /src/plugins | |
| parent | 518fa4a3d523b481e2d72e78361dd979e6c850f4 (diff) | |
| download | frippy-6ec0c444f642630485ca18b3043191b67a6f8e8c.tar.gz frippy-6ec0c444f642630485ca18b3043191b67a6f8e8c.zip | |
Factoids: Improve display of runtime errors in lua
Diffstat (limited to 'src/plugins')
| -rw-r--r-- | src/plugins/factoids/database.rs | 10 | ||||
| -rw-r--r-- | src/plugins/factoids/mod.rs | 12 | ||||
| -rw-r--r-- | src/plugins/factoids/utils.rs | 23 |
3 files changed, 26 insertions, 19 deletions
diff --git a/src/plugins/factoids/database.rs b/src/plugins/factoids/database.rs index 321931f..702834f 100644 --- a/src/plugins/factoids/database.rs +++ b/src/plugins/factoids/database.rs @@ -51,10 +51,10 @@ pub trait Database: Send + Sync { impl Database for HashMap<(String, i32), Factoid> { fn insert_factoid(&mut self, factoid: &NewFactoid) -> Result<(), FactoidsError> { let factoid = Factoid { - name: String::from(factoid.name), + name: factoid.name.to_owned(), idx: factoid.idx, - content: factoid.content.to_string(), - author: factoid.author.to_string(), + content: factoid.content.to_owned(), + author: factoid.author.to_owned(), created: factoid.created, }; @@ -66,13 +66,13 @@ impl Database for HashMap<(String, i32), Factoid> { } fn get_factoid(&self, name: &str, idx: i32) -> Result<Factoid, FactoidsError> { - Ok(self.get(&(String::from(name), idx)) + Ok(self.get(&(name.to_owned(), idx)) .cloned() .ok_or(ErrorKind::NotFound)?) } fn delete_factoid(&mut self, name: &str, idx: i32) -> Result<(), FactoidsError> { - match self.remove(&(String::from(name), idx)) { + match self.remove(&(name.to_owned(), idx)) { Some(_) => Ok(()), None => Err(ErrorKind::NotFound)?, } diff --git a/src/plugins/factoids/mod.rs b/src/plugins/factoids/mod.rs index ba3ee8a..a15b60a 100644 --- a/src/plugins/factoids/mod.rs +++ b/src/plugins/factoids/mod.rs @@ -176,7 +176,10 @@ impl<T: Database> Factoids<T> { } else { match self.run_lua(&name, &content, &command) { Ok(v) => v, - Err(e) => format!("{}", e), + Err(e) => match e { + LuaError::CallbackError { cause, .. } => cause.to_string(), + _ => e.to_string(), + }, } } } else { @@ -187,12 +190,7 @@ impl<T: Database> Factoids<T> { } } - fn run_lua( - &self, - name: &str, - code: &str, - command: &PluginCommand, - ) -> Result<String, rlua::Error> { + fn run_lua(&self, name: &str, code: &str, command: &PluginCommand) -> Result<String, LuaError> { let args = command .tokens .iter() diff --git a/src/plugins/factoids/utils.rs b/src/plugins/factoids/utils.rs index fd08da1..89f5d6c 100644 --- a/src/plugins/factoids/utils.rs +++ b/src/plugins/factoids/utils.rs @@ -3,20 +3,29 @@ extern crate reqwest; use std::thread; use std::time::Duration; -use super::rlua::prelude::*; +use super::rlua::Error as LuaError; +use super::rlua::Lua; use utils::Url; +use utils::error::ErrorKind::Connection; -use self::LuaError::RuntimeError; +use failure::Fail; pub fn download(_: &Lua, url: String) -> Result<String, LuaError> { let url = Url::from(url).max_kib(1024); match url.request() { Ok(v) => Ok(v), - Err(e) => Err(RuntimeError(format!( - "Failed to download {} - {}", - url.as_str(), - e.to_string() - ))), + Err(e) => { + let error = match e.kind() { + Connection => e.cause().unwrap().to_string(), + _ => e.to_string(), + }; + + Err(LuaError::RuntimeError(format!( + "Failed to download {} - {}", + url.as_str(), + error + ))) + } } } |
