aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils.rs
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/utils.rs
parent7cdca0b7d75172d8f900e8e755ff07ed8a72966a (diff)
downloadfrippy-d8406b107c651321c9e166abc36fc66730d965a1.tar.gz
frippy-d8406b107c651321c9e166abc36fc66730d965a1.zip
Use lossy UTF8 conversion and add a log_error function
Diffstat (limited to 'src/utils.rs')
-rw-r--r--src/utils.rs26
1 files changed, 21 insertions, 5 deletions
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);
+ }
}