Skip to content

Commit

Permalink
Now CheckForUpdateEvent will report the exception as Error property i…
Browse files Browse the repository at this point in the history
…n case of failure. This closes ravibpatel#410 and closes ravibpatel#365.
  • Loading branch information
ravibpatel committed Sep 30, 2020
1 parent 5fd30a3 commit 22f6511
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 58 deletions.
34 changes: 21 additions & 13 deletions AutoUpdater.NET/AutoUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public static class AutoUpdater
/// You can set this field to your current version if you don't want to determine the version from the assembly.
/// </summary>
public static Version InstalledVersion;

/// <summary>
/// Set it to folder path where you want to download the update file. If not provided then it defaults to Temp folder.
/// </summary>
Expand Down Expand Up @@ -359,7 +360,7 @@ private static object CheckUpdate(Assembly mainAssembly)
using (MyWebClient client = GetWebClient(BaseUri, BasicAuthXML))
{
string xml = client.DownloadString(BaseUri);

if (ParseUpdateInfoEvent == null)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(UpdateInfoEventArgs));
Expand Down Expand Up @@ -488,19 +489,26 @@ private static bool StartUpdate(object result)

private static void ShowError(Exception exception)
{
if (ReportErrors)
if (CheckForUpdateEvent != null)
{
if (exception is WebException)
{
MessageBox.Show(
Resources.UpdateCheckFailedMessage,
Resources.UpdateCheckFailedCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
CheckForUpdateEvent(new UpdateInfoEventArgs {Error = exception});
}
else
{
if (ReportErrors)
{
MessageBox.Show(exception.ToString(),
exception.GetType().ToString(), MessageBoxButtons.OK,
MessageBoxIcon.Error);
if (exception is WebException)
{
MessageBox.Show(
Resources.UpdateCheckFailedMessage,
Resources.UpdateCheckFailedCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show(exception.Message,
exception.GetType().ToString(), MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
}
Expand Down Expand Up @@ -540,7 +548,7 @@ private static void Exit()
}
}
}

if (ApplicationExitEvent != null)
{
ApplicationExitEvent();
Expand Down
11 changes: 6 additions & 5 deletions AutoUpdater.NET/UpdateInfoEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public UpdateInfoEventArgs()
/// If new update is available then returns true otherwise false.
/// </summary>
public bool IsUpdateAvailable { get; set; }

/// <summary>
/// If there is an error while checking for update then this property won't be null.
/// </summary>
[XmlIgnore]
public Exception Error { get; set; }

/// <summary>
/// Download URL of the update file.
Expand Down Expand Up @@ -72,11 +78,6 @@ public string ChangelogURL
[XmlElement("checksum")]
public CheckSum CheckSum { get; set; }

/// <summary>
/// Hash algorithm that generated the checksum provided in the XML file.
/// </summary>
public string HashingAlgorithm { get; set; }

internal static string GetURL(Uri baseUri, string url)
{
if (!string.IsNullOrEmpty(url) && Uri.IsWellFormedUriString(url, UriKind.Relative))
Expand Down
19 changes: 14 additions & 5 deletions AutoUpdaterTest/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private void AutoUpdaterOnParseUpdateInfoEvent(ParseUpdateInfoEventArgs args)

private void AutoUpdaterOnCheckForUpdateEvent(UpdateInfoEventArgs args)
{
if (args != null)
if (args.Error == null)
{
if (args.IsUpdateAvailable)
{
Expand Down Expand Up @@ -213,9 +213,18 @@ private void AutoUpdaterOnCheckForUpdateEvent(UpdateInfoEventArgs args)
}
else
{
MessageBox.Show(
@"There is a problem reaching update server. Please check your internet connection and try again later.",
@"Update Check Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
if (args.Error is WebException)
{
MessageBox.Show(
@"There is a problem reaching update server. Please check your internet connection and try again later.",
@"Update Check Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show(args.Error.Message,
args.Error.GetType().ToString(), MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}

Expand All @@ -235,4 +244,4 @@ private void ButtonCheckForUpdate_Click(object sender, EventArgs e)
AutoUpdater.Start("https://rbsoft.org/updates/AutoUpdaterTest.xml");
}
}
}
}
88 changes: 53 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,51 +310,69 @@ AutoUpdater.CheckForUpdateEvent += AutoUpdaterOnCheckForUpdateEvent;

private void AutoUpdaterOnCheckForUpdateEvent(UpdateInfoEventArgs args)
{
if (args.IsUpdateAvailable)
if (args.Error == null)
{
DialogResult dialogResult;
if (args.Mandatory.Value)
if (args.IsUpdateAvailable)
{
dialogResult =
MessageBox.Show(
$@"There is new version {args.CurrentVersion} available. You are using version {args.InstalledVersion}. This is required update. Press Ok to begin updating the application.", @"Update Available",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
else
{
dialogResult =
MessageBox.Show(
$@"There is new version {args.CurrentVersion} available. You are using version {
args.InstalledVersion
}. Do you want to update the application now?", @"Update Available",
MessageBoxButtons.YesNo,
MessageBoxIcon.Information);
}

// Uncomment the following line if you want to show standard update dialog instead.
// AutoUpdater.ShowUpdateForm(args);
if (dialogResult.Equals(DialogResult.Yes) || dialogResult.Equals(DialogResult.OK))
{
try
DialogResult dialogResult;
if (args.Mandatory.Value)
{
if (AutoUpdater.DownloadUpdate(args))
{
Application.Exit();
}
dialogResult =
MessageBox.Show(
$@"There is new version {args.CurrentVersion} available. You are using version {args.InstalledVersion}. This is required update. Press Ok to begin updating the application.", @"Update Available",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
catch (Exception exception)
else
{
MessageBox.Show(exception.Message, exception.GetType().ToString(), MessageBoxButtons.OK,
MessageBoxIcon.Error);
dialogResult =
MessageBox.Show(
$@"There is new version {args.CurrentVersion} available. You are using version {
args.InstalledVersion
}. Do you want to update the application now?", @"Update Available",
MessageBoxButtons.YesNo,
MessageBoxIcon.Information);
}

// Uncomment the following line if you want to show standard update dialog instead.
// AutoUpdater.ShowUpdateForm(args);
if (dialogResult.Equals(DialogResult.Yes) || dialogResult.Equals(DialogResult.OK))
{
try
{
if (AutoUpdater.DownloadUpdate(args))
{
Application.Exit();
}
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, exception.GetType().ToString(), MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
else
{
MessageBox.Show(@"There is no update available please try again later.", @"No update available",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show(@"There is no update available please try again later.", @"No update available",
MessageBoxButtons.OK, MessageBoxIcon.Information);
if (args.Error is WebException)
{
MessageBox.Show(
@"There is a problem reaching update server. Please check your internet connection and try again later.",
@"Update Check Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show(args.Error.Message,
args.Error.GetType().ToString(), MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
````
Expand Down

0 comments on commit 22f6511

Please sign in to comment.