aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils.rs
diff options
context:
space:
mode:
authorJokler <jokler.contact@gmail.com>2018-03-02 22:11:21 +0100
committerJokler <jokler.contact@gmail.com>2018-03-02 22:11:21 +0100
commit0b4131e8cf91ed10f24d3faed341034d518aea53 (patch)
tree09498ec2f2ec495a1b45a6762e61ed67f496c6f8 /src/utils.rs
parent0bcc7c0923852b48ebbb94ceeecc98f551fa920d (diff)
downloadfrippy-0b4131e8cf91ed10f24d3faed341034d518aea53.tar.gz
frippy-0b4131e8cf91ed10f24d3faed341034d518aea53.zip
Use Error & ErrorKind pair instead of simple enums
Each plugin should define its own errors with a respective variant in the main ErrorKind of frippy. A new procedural macro was added to reduce the boilerplate required for new error system. It can be used by deriving "Error" and adding a name for the Error via the "error" attribute. So far non of the plugins except for Url and Factoids use their own errors yet.
Diffstat (limited to 'src/utils.rs')
-rw-r--r--src/utils.rs34
1 files changed, 23 insertions, 11 deletions
diff --git a/src/utils.rs b/src/utils.rs
index cf91b37..06156be 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -4,15 +4,19 @@ use std::io::{self, Read};
use reqwest::Client;
use reqwest::header::Connection;
-use failure::Fail;
-use error::FrippyError;
+use failure::ResultExt;
+use self::error::{DownloadError, ErrorKind};
/// 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()?;
+pub fn download(url: &str, max_kib: Option<usize>) -> Result<String, DownloadError> {
+ let mut response = Client::new()
+ .get(url)
+ .header(Connection::close())
+ .send()
+ .context(ErrorKind::Connection)?;
// 100 kibibyte buffer
let mut buf = [0; 100 * 1024];
@@ -25,7 +29,7 @@ pub fn download(url: &str, max_kib: Option<usize>) -> Result<String, FrippyError
Ok(0) => break,
Ok(len) => len,
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => continue,
- Err(e) => Err(e)?,
+ Err(e) => Err(e).context(ErrorKind::Read)?,
};
bytes.extend_from_slice(&buf);
@@ -34,7 +38,7 @@ pub fn download(url: &str, max_kib: Option<usize>) -> Result<String, FrippyError
// Check if the file is too large to download
if let Some(max_kib) = max_kib {
if written > max_kib * 1024 {
- Err(FrippyError::DownloadLimit { limit: max_kib })?;
+ Err(ErrorKind::DownloadLimit)?;
}
}
}
@@ -42,12 +46,20 @@ pub fn download(url: &str, max_kib: Option<usize>) -> Result<String, FrippyError
Ok(String::from_utf8_lossy(&bytes).into_owned())
}
+pub mod error {
+ #[derive(Copy, Clone, Eq, PartialEq, Debug, Fail, Error)]
+ #[error = "DownloadError"]
+ pub enum ErrorKind {
+ /// Connection Error
+ #[fail(display = "A connection error has occured")]
+ Connection,
-pub fn log_error(e: FrippyError) {
- let mut causes = e.causes();
+ /// Read Error
+ #[fail(display = "A read error has occured")]
+ Read,
- error!("{}", causes.next().unwrap());
- for cause in causes {
- error!("caused by: {}", cause);
+ /// Reached download limit error
+ #[fail(display = "Reached download limit")]
+ DownloadLimit,
}
}