diff --git a/hammond-downloader/src/downloader.rs b/hammond-downloader/src/downloader.rs index 07ad6a6..53851aa 100644 --- a/hammond-downloader/src/downloader.rs +++ b/hammond-downloader/src/downloader.rs @@ -14,11 +14,8 @@ use std::sync::{Arc, Mutex}; use hammond_data::{EpisodeWidgetQuery, PodcastCoverQuery, Save}; use hammond_data::xdg_dirs::HAMMOND_CACHE; -use std::result; - -use failure::Error; - -type Result = result::Result; +// use failure::Error; +use errors::DownloadError; // TODO: Replace path that are of type &str with std::path. // TODO: Have a convention/document absolute/relative paths, if they should end with / or not. @@ -41,7 +38,7 @@ fn download_into( file_title: &str, url: &str, progress: Option>>, -) -> Result { +) -> Result { info!("GET request to: {}", url); // Haven't included the loop check as // Steal the Stars would tigger it as @@ -65,7 +62,7 @@ fn download_into( info!("Status Resp: {}", resp.status()); if !resp.status().is_success() { - bail!("Unexpected server response: {}", resp.status()) + return Err(DownloadError::UnexpectedResponse(resp.status())); } let headers = resp.headers().clone(); @@ -122,7 +119,7 @@ fn save_io( resp: &mut reqwest::Response, content_lenght: Option, progress: Option>>, -) -> Result<()> { +) -> Result<(), DownloadError> { info!("Downloading into: {}", file); let chunk_size = match content_lenght { Some(x) => x as usize / 99, @@ -144,7 +141,7 @@ fn save_io( if let Ok(l) = len { if let Ok(mut m) = prog.lock() { if m.should_cancel() { - bail!("Download was cancelled."); + return Err(DownloadError::DownloadCancelled); } m.set_downloaded(l); } @@ -163,7 +160,7 @@ pub fn get_episode( ep: &mut EpisodeWidgetQuery, download_folder: &str, progress: Option>>, -) -> Result<()> { +) -> Result<(), DownloadError> { // Check if its alrdy downloaded if ep.local_uri().is_some() { if Path::new(ep.local_uri().unwrap()).exists() { diff --git a/hammond-downloader/src/errors.rs b/hammond-downloader/src/errors.rs index 8ee10d3..7df3bd1 100644 --- a/hammond-downloader/src/errors.rs +++ b/hammond-downloader/src/errors.rs @@ -1,13 +1,35 @@ -use hammond_data; +use hammond_data::errors::DataError; use reqwest; use std::io; #[derive(Fail, Debug)] -pub enum DownloaderError { +pub enum DownloadError { #[fail(display = "Reqwest error: {}", _0)] - RequestError(reqwest::Error), + RequestError(#[cause] reqwest::Error), #[fail(display = "Data error: {}", _0)] - DataError(hammond_data::errors::DataError), + DataError(#[cause] DataError), #[fail(display = "Io error: {}", _0)] - IoError(io::Error), + IoError(#[cause] io::Error), + #[fail(display = "The Download was cancelled")] + DownloadCancelled, + #[fail(display = "Unexpected server response: {}", _0)] + UnexpectedResponse(reqwest::StatusCode), +} + +impl From for DownloadError { + fn from(err: reqwest::Error) -> Self { + DownloadError::RequestError(err) + } +} + +impl From for DownloadError { + fn from(err: io::Error) -> Self { + DownloadError::IoError(err) + } +} + +impl From for DownloadError { + fn from(err: DataError) -> Self { + DownloadError::DataError(err) + } } diff --git a/hammond-downloader/src/lib.rs b/hammond-downloader/src/lib.rs index 2a5f991..64276d3 100644 --- a/hammond-downloader/src/lib.rs +++ b/hammond-downloader/src/lib.rs @@ -1,7 +1,6 @@ #![recursion_limit = "1024"] #![deny(unused_extern_crates, unused)] -#[macro_use] extern crate failure; #[macro_use] extern crate failure_derive;