Explorar el Código

Handle URL opening and clipboard errors

timvisee hace 7 años
padre
commit
e84ba364dc
Se han modificado 2 ficheros con 20 adiciones y 14 borrados
  1. 6 8
      api/src/action/upload.rs
  2. 14 6
      cli/src/action/upload.rs

+ 6 - 8
api/src/action/upload.rs

@@ -4,7 +4,6 @@ use std::io::{
     Error as IoError,
 };
 use std::path::PathBuf;
-use std::result::Result as StdResult;
 use std::sync::{Arc, Mutex};
 
 use mime_guess::{guess_mime_type, Mime};
@@ -35,7 +34,6 @@ use file::metadata::{Metadata, XFileMetadata};
 
 // TODO: remove these specified types
 type EncryptedReader = ProgressReader<BufReader<EncryptedFileReader>>;
-pub type Result<T> = ::std::result::Result<T, UploadError>;
 
 /// A file upload action to a Send server.
 pub struct Upload {
@@ -60,7 +58,7 @@ impl Upload {
         self,
         client: &Client,
         reporter: Arc<Mutex<ProgressReporter>>,
-    ) -> StdResult<SendFile, Error> {
+    ) -> Result<SendFile, Error> {
         // Create file data, generate a key
         let file = FileData::from(&self.path)?;
         let key = KeySet::generate(true);
@@ -97,7 +95,7 @@ impl Upload {
 
     /// Create a blob of encrypted metadata.
     fn create_metadata(&self, key: &KeySet, file: &FileData)
-        -> StdResult<Vec<u8>, MetaError>
+        -> Result<Vec<u8>, MetaError>
     {
         // Construct the metadata
         let metadata = Metadata::from(
@@ -131,7 +129,7 @@ impl Upload {
         &self,
         key: &KeySet,
         reporter: Arc<Mutex<ProgressReporter>>,
-    ) -> StdResult<EncryptedReader, Error> {
+    ) -> Result<EncryptedReader, Error> {
         // Open the file
         let file = match File::open(self.path.as_path()) {
             Ok(file) => file,
@@ -199,7 +197,7 @@ impl Upload {
     /// Execute the given request, and create a file object that represents the
     /// uploaded file.
     fn execute_request(&self, req: Request, client: &Client, key: &KeySet) 
-        -> StdResult<SendFile, UploadError>
+        -> Result<SendFile, UploadError>
     {
         // Execute the request
         let mut response = match client.execute(req) {
@@ -254,7 +252,7 @@ impl UploadResponse {
     ///
     /// The `host` and `key` must be given.
     pub fn into_file(self, host: Url, key: &KeySet)
-        -> StdResult<SendFile, UploadError>
+        -> Result<SendFile, UploadError>
     {
         Ok(
             SendFile::new_now(
@@ -281,7 +279,7 @@ struct FileData<'a> {
 
 impl<'a> FileData<'a> {
     /// Create a file data object, from the file at the given path.
-    pub fn from(path: &'a PathBuf) -> StdResult<Self, FileError> {
+    pub fn from(path: &'a PathBuf) -> Result<Self, FileError> {
         // Make sure the given path is a file
         if !path.is_file() {
             return Err(FileError::NotAFile);

+ 14 - 6
cli/src/action/upload.rs

@@ -1,6 +1,8 @@
+use std::error::Error as StdError;
 use std::path::Path;
 use std::sync::{Arc, Mutex};
 
+use failure::{err_msg, Fail};
 use ffsend_api::action::upload::Upload as ApiUpload;
 use ffsend_api::reqwest::Client;
 
@@ -9,7 +11,7 @@ use error::ActionError;
 use progress::ProgressBar;
 use util::open_url;
 #[cfg(feature = "clipboard")]
-use util::set_clipboard;
+use util::{print_error, set_clipboard};
 
 /// A file upload action.
 pub struct Upload<'a> {
@@ -47,17 +49,23 @@ impl<'a> Upload<'a> {
 
         // Open the URL in the browser
         if self.cmd.open() {
-            // TODO: do not expect, but return an error
-            open_url(url.clone()).expect("failed to open URL");
+            if let Err(err) = open_url(url.clone()) {
+                print_error(
+                    err.context("Failed to open the URL in the browser")
+                );
+            };
         }
 
         // Copy the URL in the user's clipboard
         #[cfg(feature = "clipboard")]
         {
             if self.cmd.copy() {
-                // TODO: do not expect, but return an error
-                set_clipboard(url.as_str().to_owned())
-                    .expect("failed to put download URL in user clipboard");
+                if set_clipboard(url.as_str().to_owned()).is_err() {
+                    print_error(
+                        err_msg("Failed to copy the URL to the clipboard")
+                            .compat()
+                    );
+                }
             }
         }