Downloader: Switch to returning Downloader::Error instead of failure::Error.
This commit is contained in:
parent
4d1168803c
commit
5cd3dff1d4
@ -14,11 +14,8 @@ use std::sync::{Arc, Mutex};
|
|||||||
use hammond_data::{EpisodeWidgetQuery, PodcastCoverQuery, Save};
|
use hammond_data::{EpisodeWidgetQuery, PodcastCoverQuery, Save};
|
||||||
use hammond_data::xdg_dirs::HAMMOND_CACHE;
|
use hammond_data::xdg_dirs::HAMMOND_CACHE;
|
||||||
|
|
||||||
use std::result;
|
// use failure::Error;
|
||||||
|
use errors::DownloadError;
|
||||||
use failure::Error;
|
|
||||||
|
|
||||||
type Result<T> = result::Result<T, Error>;
|
|
||||||
|
|
||||||
// TODO: Replace path that are of type &str with std::path.
|
// 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.
|
// 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,
|
file_title: &str,
|
||||||
url: &str,
|
url: &str,
|
||||||
progress: Option<Arc<Mutex<DownloadProgress>>>,
|
progress: Option<Arc<Mutex<DownloadProgress>>>,
|
||||||
) -> Result<String> {
|
) -> Result<String, DownloadError> {
|
||||||
info!("GET request to: {}", url);
|
info!("GET request to: {}", url);
|
||||||
// Haven't included the loop check as
|
// Haven't included the loop check as
|
||||||
// Steal the Stars would tigger it as
|
// Steal the Stars would tigger it as
|
||||||
@ -65,7 +62,7 @@ fn download_into(
|
|||||||
info!("Status Resp: {}", resp.status());
|
info!("Status Resp: {}", resp.status());
|
||||||
|
|
||||||
if !resp.status().is_success() {
|
if !resp.status().is_success() {
|
||||||
bail!("Unexpected server response: {}", resp.status())
|
return Err(DownloadError::UnexpectedResponse(resp.status()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let headers = resp.headers().clone();
|
let headers = resp.headers().clone();
|
||||||
@ -122,7 +119,7 @@ fn save_io(
|
|||||||
resp: &mut reqwest::Response,
|
resp: &mut reqwest::Response,
|
||||||
content_lenght: Option<u64>,
|
content_lenght: Option<u64>,
|
||||||
progress: Option<Arc<Mutex<DownloadProgress>>>,
|
progress: Option<Arc<Mutex<DownloadProgress>>>,
|
||||||
) -> Result<()> {
|
) -> Result<(), DownloadError> {
|
||||||
info!("Downloading into: {}", file);
|
info!("Downloading into: {}", file);
|
||||||
let chunk_size = match content_lenght {
|
let chunk_size = match content_lenght {
|
||||||
Some(x) => x as usize / 99,
|
Some(x) => x as usize / 99,
|
||||||
@ -144,7 +141,7 @@ fn save_io(
|
|||||||
if let Ok(l) = len {
|
if let Ok(l) = len {
|
||||||
if let Ok(mut m) = prog.lock() {
|
if let Ok(mut m) = prog.lock() {
|
||||||
if m.should_cancel() {
|
if m.should_cancel() {
|
||||||
bail!("Download was cancelled.");
|
return Err(DownloadError::DownloadCancelled);
|
||||||
}
|
}
|
||||||
m.set_downloaded(l);
|
m.set_downloaded(l);
|
||||||
}
|
}
|
||||||
@ -163,7 +160,7 @@ pub fn get_episode(
|
|||||||
ep: &mut EpisodeWidgetQuery,
|
ep: &mut EpisodeWidgetQuery,
|
||||||
download_folder: &str,
|
download_folder: &str,
|
||||||
progress: Option<Arc<Mutex<DownloadProgress>>>,
|
progress: Option<Arc<Mutex<DownloadProgress>>>,
|
||||||
) -> Result<()> {
|
) -> Result<(), DownloadError> {
|
||||||
// Check if its alrdy downloaded
|
// Check if its alrdy downloaded
|
||||||
if ep.local_uri().is_some() {
|
if ep.local_uri().is_some() {
|
||||||
if Path::new(ep.local_uri().unwrap()).exists() {
|
if Path::new(ep.local_uri().unwrap()).exists() {
|
||||||
|
|||||||
@ -1,13 +1,35 @@
|
|||||||
use hammond_data;
|
use hammond_data::errors::DataError;
|
||||||
use reqwest;
|
use reqwest;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
#[derive(Fail, Debug)]
|
#[derive(Fail, Debug)]
|
||||||
pub enum DownloaderError {
|
pub enum DownloadError {
|
||||||
#[fail(display = "Reqwest error: {}", _0)]
|
#[fail(display = "Reqwest error: {}", _0)]
|
||||||
RequestError(reqwest::Error),
|
RequestError(#[cause] reqwest::Error),
|
||||||
#[fail(display = "Data error: {}", _0)]
|
#[fail(display = "Data error: {}", _0)]
|
||||||
DataError(hammond_data::errors::DataError),
|
DataError(#[cause] DataError),
|
||||||
#[fail(display = "Io error: {}", _0)]
|
#[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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
#![recursion_limit = "1024"]
|
#![recursion_limit = "1024"]
|
||||||
#![deny(unused_extern_crates, unused)]
|
#![deny(unused_extern_crates, unused)]
|
||||||
|
|
||||||
#[macro_use]
|
|
||||||
extern crate failure;
|
extern crate failure;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate failure_derive;
|
extern crate failure_derive;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user