소스 검색

Add error handling to publish window

Billy Barrow 1 년 전
부모
커밋
7a5ab6d7ee
1개의 변경된 파일102개의 추가작업 그리고 20개의 파일을 삭제
  1. 102 20
      src/PublishWindow.vala

+ 102 - 20
src/PublishWindow.vala

@@ -26,6 +26,10 @@ namespace Publicate {
         private Button cancel_button;
         private Button continue_button;
 
+        private Box error_view;
+        private Label error_heading;
+        private Label error_body;
+
         private Box complete;
 
         private delegate void DecisionCallback();
@@ -89,6 +93,35 @@ namespace Publicate {
             
             stack.add_child(identity_select);
 
+            error_view = new Box(Orientation.VERTICAL, 8);
+            error_view.halign = Align.CENTER;
+            error_view.valign = Align.CENTER;
+            error_view.margin_top = 18;
+            error_view.margin_start = 64;
+            error_view.margin_end = 64;
+            error_view.margin_bottom = 18;
+            error_heading.width_request = 200;
+
+            error_heading = new Label("Decision");
+            error_heading.halign = Align.START;
+            error_heading.add_css_class("title-2");
+            error_view.append(error_heading);
+
+            error_body = new Label("Details");
+            error_body.halign = Align.START;
+            error_body.hexpand = true;
+            error_body.wrap = true;
+            error_body.wrap_mode = Pango.WrapMode.WORD;
+            error_body.natural_wrap_mode = NaturalWrapMode.WORD;
+            error_view.append(error_body);
+
+            var error_close_button = new Button.with_label("Close");
+            error_close_button.hexpand = true;
+            error_close_button.clicked.connect(() => close());
+            error_view.append(error_close_button);
+
+            stack.add_child(error_view);
+
             decision = new Box(Orientation.VERTICAL, 8);
             decision.halign = Align.CENTER;
             decision.valign = Align.CENTER;
@@ -173,6 +206,14 @@ namespace Publicate {
             stack.visible_child = decision;
         }
 
+        private void show_error(string heading, string body) {
+            error_heading.set_text(heading);
+            error_body.set_text(body);
+            stack.visible_child = error_view;
+            error_bell();
+
+        }
+
         private Ppub.Collection collection;
         private CollectionConfig config;
         private Pprf.Client client;
@@ -180,7 +221,7 @@ namespace Publicate {
         private string dest_name;
         private BinaryData cid;
         private bool unpublish = false;
-        private DateTime? old_publish_timestamp;
+        private DateTime? old_publish_timestamp = null;
         public async void publish_to(CollectionConfig config, File publication_file) {
             // TODO wrap in try catch
 
@@ -193,11 +234,23 @@ namespace Publicate {
 
             pulse_loader();
             loader_status.set_text(@"Looking up $(config.domain)…");
-            client = yield window.collection_service.get_client(config);
+            try{ 
+                client = yield window.collection_service.get_client(config);
+            }
+            catch(Error e) {
+                show_error("Server not found", @"Could not find PPRF server details for $(config.domain): $(e.message)");
+                return;
+            }
             cid = new BinaryData.from_base64(config.collection_id);
 
             loader_status.set_text(@"Querying $(config.domain) for collection information…");
-            collection = yield do_in_bg<Ppub.Collection>(() => client.get_collection(cid));
+            try {
+                collection = yield do_in_bg<Ppub.Collection>(() => client.get_collection(cid));
+            }
+            catch(Error e) {
+                show_error("Could not download collection information", @"Failed to download collection information from server: $(e.message)");
+                return;
+            }
 
             loader_pulsing = false;
             loader_progress.fraction = 0;
@@ -212,11 +265,18 @@ namespace Publicate {
 
         private void select_identity(bool unpub) {
             if(unpub) {
-                old_publish_timestamp = collection.publications.first(p => p.file_name == dest_name).publication_time;
+                old_publish_timestamp = collection.publications.first_or_default(p => p.file_name == dest_name)?.publication_time;
             }
             unpublish = unpub;
             stack.visible_child = identity_select;
-            identity_list.populate_identities(collection);
+            
+            try {
+                identity_list.populate_identities(collection);
+            }
+            catch(Error e) {
+                show_error("Error loading identity list", @"There was an error retreiving a list of local identities: $(e.message)");
+                return;
+            }
             publish_button.sensitive = identity_list.has_entries;
             header_bar.show_end_title_buttons = true;
         }
@@ -242,6 +302,10 @@ namespace Publicate {
                     show_decision("Overwrite file?", @"There is already a file on this server with the name \"$dest_name\", if you continue it will be overwritten.", true, () => close(), () => upload_and_publish.begin(true));
                 }
             }
+            catch(Error e) {
+                show_error("Name registration failed", @"Could not register the name \"$(dest_name)\" with the server: $(e.message)");
+                return;
+            }
 
             yield upload_and_publish(false);
         }
@@ -251,29 +315,47 @@ namespace Publicate {
             loader_status.set_text(@"Preparing to upload publication…");
             pulse_loader();
 
-            var file_info = yield publication_file.query_info_async("*", FileQueryInfoFlags.NONE, 1);
-            var file_size = file_info.get_size();
-            var file_stream = yield publication_file.read_async(1);
-            var flags = replace_destination ? Pprf.Messages.FinaliseUploadFlags.OVERWRITE_DESTINATION : 0;
-            yield do_void_in_bg(() => client.upload(cid, file_stream, file_size, dest_name, unpublish, identity_list.selected_identity, upload_callback, flags));
+            try {
+                var file_info = yield publication_file.query_info_async("*", FileQueryInfoFlags.NONE, 1);
+                var file_size = file_info.get_size();
+                var file_stream = yield publication_file.read_async(1);
+                var flags = replace_destination ? Pprf.Messages.FinaliseUploadFlags.OVERWRITE_DESTINATION : 0;
+                yield do_void_in_bg(() => client.upload(cid, file_stream, file_size, dest_name, unpublish, identity_list.selected_identity, upload_callback, flags));
+            }
+            catch(Error e) {
+                show_error("Upload failed", @"There was an error uploading the publication to the server: $(e.message)");
+                return;
+            }
             
             pulse_loader();
             loader_status.set_text(@"Computing publication checksum…");
-            var checksum = yield do_in_bg<BinaryData>(() => new BinaryData.from_byte_array(Pprf.Util.file_checksum(publication_file)));
+            try {
+                var checksum = yield do_in_bg<BinaryData>(() => new BinaryData.from_byte_array(Pprf.Util.file_checksum(publication_file)));
 
-            loader_status.set_text(@"Signing publication…");
-            var timestamp = new DateTime.now_local();
-            if(old_publish_timestamp != null) {
-                timestamp = old_publish_timestamp;
-            }
+                loader_status.set_text(@"Signing publication…");
+                var timestamp = old_publish_timestamp;
+                if(timestamp == null) {
+                    timestamp = new DateTime.now_local();
+                }
 
-            var publication = yield do_in_bg<Ppub.CollectionPublication>(() => new Ppub.CollectionPublication(dest_name, timestamp, identity_list.selected_identity.name, identity_list.selected_credentials, checksum.to_array()));
+                var publication = yield do_in_bg<Ppub.CollectionPublication>(() => new Ppub.CollectionPublication(dest_name, timestamp, identity_list.selected_identity.name, identity_list.selected_credentials, checksum.to_array()));
 
-            loader_status.set_text(@"Publishing…");
-            yield do_void_in_bg(() => client.publish(cid, publication, identity_list.selected_identity));
+                loader_status.set_text(@"Publishing…");
+                yield do_void_in_bg(() => client.publish(cid, publication, identity_list.selected_identity));
+            }
+            catch(Error e) {
+                show_error("Publication failed", @"There was an error publishing the publication: $(e.message)");
+                return;
+            }
 
             loader_status.set_text(@"Rebuilding index…");
-            yield do_void_in_bg(() => client.rebuild_index(cid, identity_list.selected_identity));
+            try {
+                yield do_void_in_bg(() => client.rebuild_index(cid, identity_list.selected_identity));
+            }
+            catch(Error e) {
+                show_error("Index rebuild failed", @"The publication has been uploaded to the server, however there was an error rebuilding the collection's index: $(e.message)");
+                return;
+            }
 
             stack.visible_child = complete;
             loader_pulsing = false;