aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJokler <jokler.contact@gmail.com>2018-03-01 18:34:03 +0100
committerJokler <jokler.contact@gmail.com>2018-03-01 18:34:03 +0100
commitd8406b107c651321c9e166abc36fc66730d965a1 (patch)
treea9918c8a98ad3c8d32c46d8b2b1518e43743b056 /src
parent7cdca0b7d75172d8f900e8e755ff07ed8a72966a (diff)
downloadfrippy-d8406b107c651321c9e166abc36fc66730d965a1.tar.gz
frippy-d8406b107c651321c9e166abc36fc66730d965a1.zip
Use lossy UTF8 conversion and add a log_error function
Diffstat (limited to 'src')
-rw-r--r--src/error.rs11
-rw-r--r--src/main.rs9
-rw-r--r--src/plugins/factoids/mod.rs2
-rw-r--r--src/plugins/factoids/utils.rs2
-rw-r--r--src/plugins/url.rs7
-rw-r--r--src/utils.rs26
6 files changed, 26 insertions, 31 deletions
diff --git a/src/error.rs b/src/error.rs
index 674faaa..fa232be 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -1,7 +1,6 @@
//! Errors for `frippy` crate using `failure`.
use std::io::Error as IoError;
-use std::str::Utf8Error;
use irc::error::IrcError;
use reqwest::Error as ReqwestError;
#[cfg(feature = "mysql")]
@@ -30,10 +29,6 @@ pub enum FrippyError {
#[fail(display = "An I/O error occured")]
Io(#[cause] IoError),
- /// A decoding error
- #[fail(display = "Failed to decode bytes")]
- Decoding(#[cause] Utf8Error),
-
/// An r2d2 error
#[cfg(feature = "mysql")]
#[fail(display = "An r2d2 error occured")]
@@ -98,12 +93,6 @@ impl From<IoError> for FrippyError {
}
}
-impl From<Utf8Error> for FrippyError {
- fn from(e: Utf8Error) -> FrippyError {
- FrippyError::Decoding(e)
- }
-}
-
#[cfg(feature = "mysql")]
impl From<R2d2Error> for FrippyError {
fn from(e: R2d2Error) -> FrippyError {
diff --git a/src/main.rs b/src/main.rs
index 461387d..3432e3e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -5,7 +5,6 @@ extern crate frippy;
extern crate glob;
extern crate irc;
extern crate time;
-extern crate failure;
#[cfg(feature = "mysql")]
extern crate diesel;
@@ -27,7 +26,6 @@ use log::{Level, LevelFilter, Metadata, Record};
use irc::client::reactor::IrcReactor;
use glob::glob;
-use failure::Fail;
use frippy::plugins;
use frippy::Config;
@@ -72,12 +70,7 @@ static LOGGER: Logger = Logger;
fn main() {
// Print any errors that caused frippy to shut down
if let Err(e) = run() {
- let mut causes = e.causes();
-
- error!("{}", causes.next().unwrap());
- for cause in causes {
- error!("caused by: {}", cause);
- }
+ frippy::utils::log_error(e);
};
}
diff --git a/src/plugins/factoids/mod.rs b/src/plugins/factoids/mod.rs
index 3f89943..806bb7e 100644
--- a/src/plugins/factoids/mod.rs
+++ b/src/plugins/factoids/mod.rs
@@ -83,7 +83,7 @@ impl<T: Database> Factoids<T> {
let name = command.tokens.remove(0);
let url = &command.tokens[0];
- let content = ::utils::download(1024, url)?;
+ let content = ::utils::download(url, Some(1024))?;
Ok(self.create_factoid(&name, &content, &command.source)?)
}
diff --git a/src/plugins/factoids/utils.rs b/src/plugins/factoids/utils.rs
index 036dcc6..009b46b 100644
--- a/src/plugins/factoids/utils.rs
+++ b/src/plugins/factoids/utils.rs
@@ -9,7 +9,7 @@ use super::rlua::prelude::*;
use self::LuaError::RuntimeError;
pub fn download(_: &Lua, url: String) -> Result<String, LuaError> {
- match utils::download(1024, &url) {
+ match utils::download(&url, Some(1024)) {
Ok(v) => Ok(v),
Err(e) => Err(RuntimeError(format!("Failed to download {} - {}", url, e.to_string()))),
}
diff --git a/src/plugins/url.rs b/src/plugins/url.rs
index af6f36f..6f00466 100644
--- a/src/plugins/url.rs
+++ b/src/plugins/url.rs
@@ -50,7 +50,7 @@ impl Url {
fn url(&self, text: &str) -> Result<String, FrippyError> {
let url = self.grep_url(text).ok_or(UrlError::MissingUrl)?;
- let body = utils::download(self.max_kib, &url)?;
+ let body = utils::download(&url, Some(self.max_kib))?;
Ok(self.get_title(&body).ok_or(UrlError::MissingTitle)?)
}
@@ -72,10 +72,7 @@ impl Plugin for Url {
match message.command {
Command::PRIVMSG(_, ref content) => match self.url(content) {
Ok(title) => client.send_privmsg(message.response_target().unwrap(), &title),
- Err(e) => {
- error!("Url plugin error: {}", e);
- Ok(())
- }
+ Err(e) => Ok(utils::log_error(e)),
},
_ => Ok(()),
}
diff --git a/src/utils.rs b/src/utils.rs
index 1f3a117..cf91b37 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -4,9 +4,14 @@ use std::io::{self, Read};
use reqwest::Client;
use reqwest::header::Connection;
+use failure::Fail;
use error::FrippyError;
-pub fn download(max_kib: usize, url: &str) -> Result<String, FrippyError> {
+/// Downloads the file and converts it to a String.
+/// Any invalid bytes are converted to a replacement character.
+///
+/// The error indicated either a failed download or that the DownloadLimit was reached
+pub fn download(url: &str, max_kib: Option<usize>) -> Result<String, FrippyError> {
let mut response = Client::new().get(url).header(Connection::close()).send()?;
// 100 kibibyte buffer
@@ -27,11 +32,22 @@ pub fn download(max_kib: usize, url: &str) -> Result<String, FrippyError> {
written += len;
// Check if the file is too large to download
- if written > max_kib * 1024 {
- Err(FrippyError::DownloadLimit { limit: max_kib })?;
+ if let Some(max_kib) = max_kib {
+ if written > max_kib * 1024 {
+ Err(FrippyError::DownloadLimit { limit: max_kib })?;
+ }
}
}
- let body = str::from_utf8(&bytes)?;
- Ok(body.to_string())
+ Ok(String::from_utf8_lossy(&bytes).into_owned())
+}
+
+
+pub fn log_error(e: FrippyError) {
+ let mut causes = e.causes();
+
+ error!("{}", causes.next().unwrap());
+ for cause in causes {
+ error!("caused by: {}", cause);
+ }
}