diff options
| author | Jokler <jokler.contact@gmail.com> | 2018-02-28 01:21:25 +0100 |
|---|---|---|
| committer | Jokler <jokler.contact@gmail.com> | 2018-02-28 01:21:25 +0100 |
| commit | b40a984ed9b6a948265e1287ecc08f4e16c64ecd (patch) | |
| tree | 6b94762899961de9da57e426785c29d401b48442 /src/utils.rs | |
| parent | 8ea98aab1ea34e0213380082577e2a3ff2d3aa2e (diff) | |
| download | frippy-b40a984ed9b6a948265e1287ecc08f4e16c64ecd.tar.gz frippy-b40a984ed9b6a948265e1287ecc08f4e16c64ecd.zip | |
Create errors with failure
The plugins are mostly not using the new errors yet and
the error handling in the Factoids plugin is just temporary.
Diffstat (limited to 'src/utils.rs')
| -rw-r--r-- | src/utils.rs | 101 |
1 files changed, 43 insertions, 58 deletions
diff --git a/src/utils.rs b/src/utils.rs index 99b04c4..68ad4d8 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,73 +1,58 @@ -extern crate reqwest; - use std::str; use std::io::{self, Read}; -use self::reqwest::Client; -use self::reqwest::header::Connection; +use reqwest::Client; +use reqwest::header::Connection; -pub fn download(max_kib: usize, url: &str) -> Option<String> { - let response = Client::new().get(url).header(Connection::close()).send(); +use error::FrippyError; - match response { - Ok(mut response) => { - let mut body = String::new(); +pub fn download(max_kib: usize, url: &str) -> Result<String, FrippyError> { + let mut response = Client::new().get(url).header(Connection::close()).send()?; - // 100 kibibyte buffer - let mut buf = [0; 100 * 1024]; - let mut written = 0; - let mut vec = Vec::new(); - let mut end_of_valid = None; + let mut body = String::new(); - // Read until we reach EOF or max_kib KiB - loop { - if let Some(eov) = end_of_valid { - vec = vec[..eov].to_vec(); - } + // 100 kibibyte buffer + let mut buf = [0; 100 * 1024]; + let mut written = 0; + let mut vec = Vec::new(); + let mut end_of_valid = None; - let len = match response.read(&mut buf) { - Ok(0) => break, - Ok(len) => len, - Err(ref e) if e.kind() == io::ErrorKind::Interrupted => continue, - Err(e) => { - debug!("Download from {:?} failed: {}", url, e); - return None; - } - }; - vec.extend_from_slice(&buf); + // Read until we reach EOF or max_kib KiB + loop { + if let Some(eov) = end_of_valid { + vec = vec[..eov].to_vec(); + } - end_of_valid = None; - let body_slice = match str::from_utf8(&vec[..len]) { - Ok(slice) => slice, - Err(e) => { - let valid = e.valid_up_to(); - if valid == 0 { - error!("Failed to read bytes from {:?} as UTF8: {}", url, e); - return None; - } - end_of_valid = Some(valid); + let len = match response.read(&mut buf) { + Ok(0) => break, + Ok(len) => len, + Err(ref e) if e.kind() == io::ErrorKind::Interrupted => continue, + Err(e) => Err(e)?, + }; + vec.extend_from_slice(&buf); + + end_of_valid = None; + let body_slice = match str::from_utf8(&vec[..len]) { + Ok(slice) => slice, + Err(e) => { + let valid = e.valid_up_to(); + if valid == 0 { + Err(e)?; + } + end_of_valid = Some(valid); - str::from_utf8(&buf[..valid]).unwrap() - } - }; + str::from_utf8(&buf[..valid])? + } + }; - body.push_str(body_slice); - written += len; + body.push_str(body_slice); + written += len; - // Check if the file is too large to download - if written > max_kib * 1024 { - debug!( - "Stopping download - File from {:?} is larger than {} KiB", - url, max_kib - ); - return None; - } - } - Some(body) - } - Err(e) => { - debug!("Bad response from {:?}: ({})", url, e); - None + // Check if the file is too large to download + if written > max_kib * 1024 { + Err(FrippyError::DownloadLimit { limit: max_kib })?; } } + + Ok(body) } |
