Properly report file exist (check) errors

This commit is contained in:
timvisee 2018-04-11 00:45:51 +02:00
parent c79a2c2298
commit 1dbb1ee40b
No known key found for this signature in database
GPG key ID: 109CBA0BF74036C2
3 changed files with 67 additions and 27 deletions

View file

@ -1,16 +1,21 @@
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use clap::ArgMatches; use clap::ArgMatches;
use ffsend_api::action::download::Download as ApiDownload; use ffsend_api::action::download::{
use ffsend_api::action::exists::Exists as ApiExists; Download as ApiDownload,
use ffsend_api::file::remote_file::RemoteFile; Error as DownloadError,
};
use ffsend_api::action::exists::{
Error as ExistsError,
Exists as ApiExists,
};
use ffsend_api::file::remote_file::{FileParseError, RemoteFile};
use ffsend_api::reqwest::Client; use ffsend_api::reqwest::Client;
use cmd::matcher::{ use cmd::matcher::{
Matcher, Matcher,
download::DownloadMatcher, download::DownloadMatcher,
}; };
use error::ActionError;
use progress::ProgressBar; use progress::ProgressBar;
use util::prompt_password; use util::prompt_password;
@ -29,7 +34,7 @@ impl<'a> Download<'a> {
/// Invoke the download action. /// Invoke the download action.
// TODO: create a trait for this method // TODO: create a trait for this method
pub fn invoke(&self) -> Result<(), ActionError> { pub fn invoke(&self) -> Result<(), Error> {
// Create the command matchers // Create the command matchers
let matcher_download = DownloadMatcher::with(self.cmd_matches).unwrap(); let matcher_download = DownloadMatcher::with(self.cmd_matches).unwrap();
@ -47,9 +52,13 @@ impl<'a> Download<'a> {
let target = matcher_download.output(); let target = matcher_download.output();
let mut password = matcher_download.password(); let mut password = matcher_download.password();
// Check whether the file exists
let exists = ApiExists::new(&file).invoke(&client)?;
if !exists.exists() {
return Err(Error::Expired);
}
// Check whether the file requires a password // Check whether the file requires a password
// TODO: do not unwrap
let exists = ApiExists::new(&file).invoke(&client).unwrap();
if exists.has_password() != password.is_some() { if exists.has_password() != password.is_some() {
if exists.has_password() { if exists.has_password() {
println!("This file is protected with a password."); println!("This file is protected with a password.");
@ -77,3 +86,41 @@ impl<'a> Download<'a> {
Ok(()) Ok(())
} }
} }
#[derive(Debug, Fail)]
pub enum Error {
/// Failed to parse a share URL, it was invalid.
/// This error is not related to a specific action.
#[fail(display = "Invalid share URL")]
InvalidUrl(#[cause] FileParseError),
/// An error occurred while checking if the file exists.
#[fail(display = "Failed to check whether the file exists")]
Exists(#[cause] ExistsError),
/// An error occurred while downloading the file.
#[fail(display = "")]
Download(#[cause] DownloadError),
/// The given Send file has expired, or did never exist in the first place.
#[fail(display = "The file has expired or did never exist")]
Expired,
}
impl From<FileParseError> for Error {
fn from(err: FileParseError) -> Error {
Error::InvalidUrl(err)
}
}
impl From<ExistsError> for Error {
fn from(err: ExistsError) -> Error {
Error::Exists(err)
}
}
impl From<DownloadError> for Error {
fn from(err: DownloadError) -> Error {
Error::Download(err)
}
}

View file

@ -53,8 +53,7 @@ impl<'a> Info<'a> {
// TODO: show an informative error if the owner token isn't set // TODO: show an informative error if the owner token isn't set
// Check whether the file exists // Check whether the file exists
// TODO: do not unwrap let exists = ApiExists::new(&file).invoke(&client)?;
let exists = ApiExists::new(&file).invoke(&client).unwrap();
if !exists.exists() { if !exists.exists() {
return Err(Error::Expired); return Err(Error::Expired);
} }

View file

@ -1,12 +1,12 @@
use ffsend_api::action::delete::Error as DeleteError; use ffsend_api::action::delete::Error as DeleteError;
use ffsend_api::action::download::Error as DownloadError;
use ffsend_api::action::exists::Error as ExistsError; use ffsend_api::action::exists::Error as ExistsError;
use ffsend_api::action::params::Error as ParamsError; use ffsend_api::action::params::Error as ParamsError;
use ffsend_api::action::password::Error as PasswordError; use ffsend_api::action::password::Error as PasswordError;
use ffsend_api::action::upload::Error as UploadError; use ffsend_api::action::upload::Error as UploadError;
use ffsend_api::file::remote_file::FileParseError; use ffsend_api::file::remote_file::FileParseError;
use action::info::Error as InfoError; use action::download::Error as CliDownloadError;
use action::info::Error as CliInfoError;
#[derive(Fail, Debug)] #[derive(Fail, Debug)]
pub enum Error { pub enum Error {
@ -15,8 +15,14 @@ pub enum Error {
Action(#[cause] ActionError), Action(#[cause] ActionError),
} }
impl From<InfoError> for Error { impl From<CliDownloadError> for Error {
fn from(err: InfoError) -> Error { fn from(err: CliDownloadError) -> Error {
Error::Action(ActionError::Download(err))
}
}
impl From<CliInfoError> for Error {
fn from(err: CliInfoError) -> Error {
Error::Action(ActionError::Info(err)) Error::Action(ActionError::Info(err))
} }
} }
@ -35,7 +41,7 @@ pub enum ActionError {
/// An error occurred while invoking the download action. /// An error occurred while invoking the download action.
#[fail(display = "Failed to download the requested file")] #[fail(display = "Failed to download the requested file")]
Download(#[cause] DownloadError), Download(#[cause] CliDownloadError),
/// An error occurred while invoking the exists action. /// An error occurred while invoking the exists action.
#[fail(display = "Failed to check whether the file exists")] #[fail(display = "Failed to check whether the file exists")]
@ -43,7 +49,7 @@ pub enum ActionError {
/// An error occurred while invoking the info action. /// An error occurred while invoking the info action.
#[fail(display = "Failed to fetch file info")] #[fail(display = "Failed to fetch file info")]
Info(#[cause] InfoError), Info(#[cause] CliInfoError),
/// An error occurred while invoking the params action. /// An error occurred while invoking the params action.
#[fail(display = "Failed to change the parameters")] #[fail(display = "Failed to change the parameters")]
@ -70,24 +76,12 @@ impl From<DeleteError> for ActionError {
} }
} }
impl From<DownloadError> for ActionError {
fn from(err: DownloadError) -> ActionError {
ActionError::Download(err)
}
}
impl From<ExistsError> for ActionError { impl From<ExistsError> for ActionError {
fn from(err: ExistsError) -> ActionError { fn from(err: ExistsError) -> ActionError {
ActionError::Exists(err) ActionError::Exists(err)
} }
} }
impl From<InfoError> for ActionError {
fn from(err: InfoError) -> ActionError {
ActionError::Info(err)
}
}
impl From<ParamsError> for ActionError { impl From<ParamsError> for ActionError {
fn from(err: ParamsError) -> ActionError { fn from(err: ParamsError) -> ActionError {
ActionError::Params(err) ActionError::Params(err)