Skip to content

Commit

Permalink
more error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
setsumi committed May 29, 2023
1 parent 83efb58 commit f4cd937
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/syosetuDownloaderCore/MessageForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/syosetuDownloaderCore/MessageForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
}
10 changes: 7 additions & 3 deletions src/syosetuDownloaderCore/Syousetsu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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}");
}
}
}
Expand Down

0 comments on commit f4cd937

Please sign in to comment.