Skip to content

Commit

Permalink
Add top-level exception handler, unwrap AggregateException. Closes #6
Browse files Browse the repository at this point in the history
Sometimes WebExceptions seem to be thrown as-is, while other times they
are wrapped in an AggregateException. Not entirely sure why, but now we
catch wrapped WebExceptions as well.

Additionally, with a new top-level exception handler the program
shouldn't outright crash anymore if an unhandled error occurs. Instead
it provides a pop-up that lets the user retrieve the error-message and
stacktrace that can then be included in a github issue.
  • Loading branch information
Jameak committed Jun 7, 2020
1 parent 7b586ff commit 5d348be
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/DataAccess/Helpers/ImageHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static async Task<byte[]> GetWebContent(string url)
}
catch (TaskCanceledException e)
{
throw new WebException("Connection lost", e);
throw new WebException("Connection lost.", e);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/DataAccess/Properties/Settings.Designer.cs

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

6 changes: 3 additions & 3 deletions src/DataAccess/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
<Value Profile="(Default)">0</Value>
</Setting>
<Setting Name="RedditUserAgent" Type="System.String" Scope="Application">
<Value Profile="(Default)">PC:ImageDownloader:v1.2.0 (by /u/Jameak)</Value>
<Value Profile="(Default)">PC:ImageDownloader:v1.2.1 (by /u/Jameak)</Value>
</Setting>
<Setting Name="DeviantartUserAgent" Type="System.String" Scope="Application">
<Value Profile="(Default)">PC:ImageDownloader:v1.2.0 (github.com/jameak/ImageDownloader)</Value>
<Value Profile="(Default)">PC:ImageDownloader:v1.2.1 (github.com/jameak/ImageDownloader)</Value>
</Setting>
<Setting Name="RedditAppId" Type="System.String" Scope="Application">
<Value Profile="(Default)">J687G-r-0ZTQsg</Value>
Expand All @@ -39,7 +39,7 @@
<Value Profile="(Default)">0fed969f9f62557</Value>
</Setting>
<Setting Name="VersionNumber" Type="System.String" Scope="Application">
<Value Profile="(Default)">v1.2.0</Value>
<Value Profile="(Default)">v1.2.1</Value>
</Setting>
<Setting Name="GitHubUserAgent" Type="System.String" Scope="Application">
<Value Profile="(Default)">ImageDownloader</Value>
Expand Down
6 changes: 3 additions & 3 deletions src/DataAccess/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@
<applicationSettings>
<DataAccess.Properties.Settings>
<setting name="RedditUserAgent" serializeAs="String">
<value>PC:ImageDownloader:v1.2.0 (by /u/Jameak)</value>
<value>PC:ImageDownloader:v1.2.1 (by /u/Jameak)</value>
</setting>
<setting name="DeviantartUserAgent" serializeAs="String">
<value>PC:ImageDownloader:v1.2.0 (github.com/jameak/ImageDownloader)</value>
<value>PC:ImageDownloader:v1.2.1 (github.com/jameak/ImageDownloader)</value>
</setting>
<setting name="RedditAppId" serializeAs="String">
<value>J687G-r-0ZTQsg</value>
Expand All @@ -53,7 +53,7 @@
<value>0fed969f9f62557</value>
</setting>
<setting name="VersionNumber" serializeAs="String">
<value>v1.2.0</value>
<value>v1.2.1</value>
</setting>
<setting name="GitHubUserAgent" serializeAs="String">
<value>ImageDownloader</value>
Expand Down
11 changes: 11 additions & 0 deletions src/Logic/Handlers/ImgurHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ await Task.Run(() =>
{
WriteToLog(sync, outputLog, $"IO Failure - Error occured while saving image: {imageName}");
}
catch (AggregateException ex)
{
if (ex.InnerException is WebException)
{
WriteToLog(sync, outputLog, $"Unable to download image: {imageName}");
}
else
{
WriteToLog(sync, outputLog, $"Unknown error occured for {imageName}. Error message is: " + ex.InnerException.Message);
}
}
}
});
}
Expand Down
14 changes: 13 additions & 1 deletion src/Logic/Handlers/RedditHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,19 @@ await Task.Run(() =>
}
catch (IOException)
{
WriteToLog(sync, outputLog, $"IO Failure - Error occured while saving image: {imageName}");
WriteToLog(sync, outputLog,
$"IO Failure - Error occured while saving image: {imageName}");
}
catch (AggregateException ex)
{
if (ex.InnerException is WebException)
{
WriteToLog(sync, outputLog, $"Unable to download image: {imageName}");
}
else
{
WriteToLog(sync, outputLog, $"Unknown error occured for {imageName}. Error message is: " + ex.InnerException.Message);
}
}
}
});
Expand Down
26 changes: 23 additions & 3 deletions src/UI/ViewModels/ImgurControlViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,29 @@ public override async void StartDownload()
else
{
ProgressMaxValue = content.GetImages().Count();
await
_downloader.FetchContent(content, TargetFolder, ResolutionFilter,
Log = new ThreadsafeObservableStringCollection());
try
{
await
_downloader.FetchContent(content, TargetFolder, ResolutionFilter,
Log = new ThreadsafeObservableStringCollection());
}
catch (Exception e)
{
Log.Add("Unhandled error occurred during downloading. Aborting.");
var message = "Unhandled error occurred during download.\n" +
"Downloading has been aborted.\n\n" +
"To get the the error-message and stacktrace\n" +
"copied to your clipboard, click 'OK'.\n" +
"If you want to report this error, please provide\n" +
"the error message in an issue on\n" +
"Github.com/Jameak/ImageDownloader";
var result = MessageBox.Show(message, "ImageDownloader", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
Clipboard.SetText(e.ToString());
MessageBox.Show("Error message copied to clipboard", "ImageDownloader", MessageBoxButton.OK);
}
}
}

IsIdle = true;
Expand Down
22 changes: 21 additions & 1 deletion src/UI/ViewModels/LocalControlViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,27 @@ public override async void StartDownload()
else
{
ProgressMaxValue = content.GetImages().Count();
await _downloader.FetchContent(content, TargetFolder, ResolutionFilter, Log, PreserveFolderHierarchy);
try
{
await _downloader.FetchContent(content, TargetFolder, ResolutionFilter, Log, PreserveFolderHierarchy);
}
catch (Exception e)
{
Log.Add("Unhandled error occurred during operation. Aborting.");
var message = "Unhandled error occurred during operation.\n" +
"Operation has been aborted.\n\n" +
"To get the the error-message and stacktrace\n" +
"copied to your clipboard, click 'OK'.\n" +
"If you want to report this error, please provide\n" +
"the error message in an issue on\n" +
"Github.com/Jameak/ImageDownloader";
var result = MessageBox.Show(message, "ImageDownloader", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
Clipboard.SetText(e.ToString());
MessageBox.Show("Exception text copied to clipboard", "ImageDownloader", MessageBoxButton.OK);
}
}
}

IsIdle = true;
Expand Down
24 changes: 22 additions & 2 deletions src/UI/ViewModels/RedditControlViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,28 @@ public override async void StartDownload()
else
{
ProgressMaxValue = content.GetImages().Count();
await
_downloader.FetchContent(content, TargetFolder, Filter, Log, SaveAlbumInNestedFolder);
try
{
await
_downloader.FetchContent(content, TargetFolder, Filter, Log, SaveAlbumInNestedFolder);
}
catch (Exception e)
{
Log.Add("Unhandled error occurred during downloading. Aborting.");
var message = "Unhandled error occurred during download.\n" +
"Downloading has been aborted.\n\n" +
"To get the the error-message and stacktrace\n" +
"copied to your clipboard, click 'OK'.\n" +
"If you want to report this error, please provide\n" +
"the error message in an issue on\n" +
"Github.com/Jameak/ImageDownloader";
var result = MessageBox.Show(message, "ImageDownloader", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
Clipboard.SetText(e.ToString());
MessageBox.Show("Exception text copied to clipboard", "ImageDownloader", MessageBoxButton.OK);
}
}
}

IsIdle = true;
Expand Down

0 comments on commit 5d348be

Please sign in to comment.