Downloader: Switch to returning Downloader::Error instead of failure::Error.

This commit is contained in:
Jordan Petridis 2018-02-05 20:41:45 +02:00
parent 4d1168803c
commit 5cd3dff1d4
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
3 changed files with 34 additions and 16 deletions

View File

@ -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<T> = result::Result<T, Error>;
// 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<Arc<Mutex<DownloadProgress>>>,
) -> Result<String> {
) -> Result<String, DownloadError> {
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<u64>,
progress: Option<Arc<Mutex<DownloadProgress>>>,
) -> 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<Arc<Mutex<DownloadProgress>>>,
) -> Result<()> {
) -> Result<(), DownloadError> {
// Check if its alrdy downloaded
if ep.local_uri().is_some() {
if Path::new(ep.local_uri().unwrap()).exists() {

View File

@ -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<reqwest::Error> for DownloadError {
fn from(err: reqwest::Error) -> Self {
DownloadError::RequestError(err)
}
}
impl From<io::Error> for DownloadError {
fn from(err: io::Error) -> Self {
DownloadError::IoError(err)
}
}
impl From<DataError> for DownloadError {
fn from(err: DataError) -> Self {
DownloadError::DataError(err)
}
}

View File

@ -1,7 +1,6 @@
#![recursion_limit = "1024"]
#![deny(unused_extern_crates, unused)]
#[macro_use]
extern crate failure;
#[macro_use]
extern crate failure_derive;