From f4cd93775f04aa32823f5a1ce11e2a9387a74654 Mon Sep 17 00:00:00 2001 From: setsumi Date: Mon, 29 May 2023 19:10:06 +1000 Subject: [PATCH] more error handling --- src/syosetuDownloaderCore/MessageForm.Designer.cs | 4 +++- src/syosetuDownloaderCore/MessageForm.cs | 12 ++++++++++++ src/syosetuDownloaderCore/Syousetsu.cs | 10 +++++++--- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/syosetuDownloaderCore/MessageForm.Designer.cs b/src/syosetuDownloaderCore/MessageForm.Designer.cs index b2085fc..0d7b895 100644 --- a/src/syosetuDownloaderCore/MessageForm.Designer.cs +++ b/src/syosetuDownloaderCore/MessageForm.Designer.cs @@ -65,10 +65,12 @@ private void InitializeComponent() this.ClientSize = new System.Drawing.Size(744, 240); this.Controls.Add(this.listboxLog); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); + this.KeyPreview = true; this.Name = "MessageForm"; - this.Text = "Messages"; + this.Text = "Messages (Ctrl+C - copy)"; this.TopMost = true; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MessageForm_FormClosing); + this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.MessageForm_KeyDown); this.ResumeLayout(false); } diff --git a/src/syosetuDownloaderCore/MessageForm.cs b/src/syosetuDownloaderCore/MessageForm.cs index bfadb3d..06fe8c1 100644 --- a/src/syosetuDownloaderCore/MessageForm.cs +++ b/src/syosetuDownloaderCore/MessageForm.cs @@ -46,5 +46,17 @@ private void MessageForm_FormClosing(object sender, FormClosingEventArgs e) e.Cancel = true; // Cancel the closure Hide(); // Hide the window } + + private void MessageForm_KeyDown(object sender, KeyEventArgs e) + { + if (e.Control == true && e.KeyCode == Keys.C) + { + if (listboxLog.SelectedItems.Count > 0) + { + string s = listboxLog.SelectedItem.ToString(); + Clipboard.SetData(DataFormats.StringFormat, s); + } + } + } } } diff --git a/src/syosetuDownloaderCore/Syousetsu.cs b/src/syosetuDownloaderCore/Syousetsu.cs index b794ee8..1429daa 100644 --- a/src/syosetuDownloaderCore/Syousetsu.cs +++ b/src/syosetuDownloaderCore/Syousetsu.cs @@ -103,12 +103,14 @@ public static CancellationTokenSource AddDownloadJob(Syousetsu.Constants details } private static syosetuDownloaderCore.MessageForm _messageForm = null; - public static void Error(Exception ex) + public static void Error(Exception ex, string prefix = "") { System.Windows.Application.Current.Dispatcher.InvokeAsync(() => { if (_messageForm == null) _messageForm = new syosetuDownloaderCore.MessageForm(); - _messageForm.Error($"{DateTime.Now} ErrorType = {ex.GetType()}; ErrorMessage = {ex.Message}"); + + prefix = string.IsNullOrEmpty(prefix) ? " " : $" {prefix} "; + _messageForm.Error($"{DateTime.Now}{prefix}ErrorType = {ex.GetType()}; ErrorMessage = {ex.Message}"); }, System.Windows.Threading.DispatcherPriority.Normal); } @@ -215,12 +217,14 @@ public static string GetNovelBody(HtmlDocument doc, Constants details) img.SetAttributeValue("src", imageSrc); } } - catch (Exception) + catch (Exception ex) { byte[] fileBytes = File.ReadAllBytes(Path.Combine(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName), "image-error.png")); string base64String = Convert.ToBase64String(fileBytes); string imageSrc = string.Format("data:image/png;base64,{0}", base64String); img.SetAttributeValue("src", imageSrc); + + Error(ex, $"Can't get image {src}"); } } }