Automatically cast errors where possible

This commit is contained in:
timvisee 2018-03-28 19:00:10 +02:00
parent 620e03c6f8
commit ba7d6a6ad1
No known key found for this signature in database
GPG key ID: 109CBA0BF74036C2
7 changed files with 58 additions and 21 deletions

View file

@ -56,8 +56,7 @@ impl<'a> Download<'a> {
let auth_nonce = self.fetch_auth_nonce(client)?;
// Fetch the meta nonce, set the input vector
let meta_nonce = self.fetch_meta_nonce(&client, &mut key, auth_nonce)
.map_err(|err| Error::Request(RequestError::Meta(err)))?;
let meta_nonce = self.fetch_meta_nonce(&client, &mut key, auth_nonce)?;
// Open the file we will write to
// TODO: this should become a temporary file first
@ -67,8 +66,7 @@ impl<'a> Download<'a> {
.map_err(|err| Error::File(path.into(), FileError::Create(err)))?;
// Create the file reader for downloading
let (reader, len) = self.create_file_reader(&key, meta_nonce, &client)
.map_err(|err| Error::Download(err))?;
let (reader, len) = self.create_file_reader(&key, meta_nonce, &client)?;
// Create the file writer
let writer = self.create_file_writer(
@ -79,8 +77,7 @@ impl<'a> Download<'a> {
).map_err(|err| Error::File(path.into(), err))?;
// Download the file
self.download(reader, writer, len, reporter)
.map_err(|err| Error::Download(err))?;
self.download(reader, writer, len, reporter)?;
// TODO: return the file path
// TODO: return the new remote state (does it still exist remote)
@ -373,6 +370,18 @@ impl From<AuthError> for Error {
}
}
impl From<MetaError> for Error {
fn from(err: MetaError) -> Error {
Error::Request(RequestError::Meta(err))
}
}
impl From<DownloadError> for Error {
fn from(err: DownloadError) -> Error {
Error::Download(err)
}
}
#[derive(Fail, Debug)]
pub enum RequestError {
/// Failed authenticating, in order to fetch the file data.

View file

@ -221,7 +221,6 @@ impl Upload {
};
// Transform the responce into a file object
// TODO: do some error handling in this into_file method
Ok(response.into_file(self.host.clone(), &key)?)
}
}
@ -258,8 +257,7 @@ impl UploadResponse {
SendFile::new_now(
self.id,
host,
Url::parse(&self.url)
.map_err(|err| UploadError::ParseUrl(err))?,
Url::parse(&self.url)?,
key.secret().to_vec(),
self.owner,
)
@ -319,7 +317,7 @@ pub enum Error {
/// An error occurred while opening, reading or using the file that
/// the should be uploaded.
// TODO: maybe append the file path here for further information
#[fail(display = "Failed to use the file to upload")]
#[fail(display = "")]
File(#[cause] FileError),
/// An error occurred while uploading the file.
@ -385,7 +383,7 @@ pub enum ReaderError {
#[derive(Fail, Debug)]
pub enum FileError {
/// The given path, is not not a file or doesn't exist.
#[fail(display = "The path is not an existing file")]
#[fail(display = "The given path is not an existing file")]
NotAFile,
/// Failed to open the file that must be uploaded for reading.
@ -417,3 +415,9 @@ pub enum UploadError {
#[fail(display = "Failed to parse received URL")]
ParseUrl(#[cause] UrlParseError),
}
impl From<UrlParseError> for UploadError {
fn from(err: UrlParseError) -> UploadError {
UploadError::ParseUrl(err)
}
}