Add non-connection failsafe

Added a try/catch as a failsafe for non-operational networks and any other errors involving using Octokit to check for updates on github. Fixes #3.
This commit is contained in:
Jordan Wages 2019-01-05 20:39:52 -06:00
parent 43c3db77d8
commit 2d366141a9
4 changed files with 56 additions and 47 deletions

View File

@ -53,17 +53,18 @@ namespace butterflow_ui
InitializeComponent();
// Check for updates.
if (OctokitWrapper.CurrentVersionStatus == OctokitWrapper.VersionStatus.behind)
if (OctokitWrapper.CurrentVersionStatus == OctokitWrapper.VersionStatus.Behind)
{
var updateMessageBoxResult = MessageBox.Show(string.Format("{0} {1}", Localization.Localization.BehindVersionStatusDescription, Localization.Localization.BehindVersionQuestion), Localization.Localization.UpdateAvailableLabel, MessageBoxButton.YesNo, MessageBoxImage.Information);
// If the user wants to update now, take them to the latest release on github and close this window.
if(updateMessageBoxResult == MessageBoxResult.Yes)
if (updateMessageBoxResult == MessageBoxResult.Yes)
{
Process.Start("https://github.com/wagesj45/butterflow-ui/releases/latest");
this.Close();
}
}
}
#region Methods
@ -113,8 +114,8 @@ namespace butterflow_ui
this.OptionsConfiguration.Height = e.Value;
break;
case ButterflowWrapper.ButterflowOutputType.Progress:
// This case doesn't need to be considered since we're binding the progress bar's value to a property on the butterflow wrapper.
// We may use this in the future, though.
// This case doesn't need to be considered since we're binding the progress bar's value to a property on the butterflow wrapper.
// We may use this in the future, though.
default:
break;
}
@ -357,7 +358,7 @@ namespace butterflow_ui
{
var button = sender as Button;
if(button != null)
if (button != null)
{
if (button.Tag.GetType() == typeof(Guid))
{
@ -385,7 +386,7 @@ namespace butterflow_ui
{
var binaryFormatter = new BinaryFormatter();
var file = binaryFormatter.Deserialize(ofd.OpenFile());
if(file is OptionsConfigurationFile)
if (file is OptionsConfigurationFile)
{
this.OptionsConfiguration.LoadFile((OptionsConfigurationFile)file);
}

View File

@ -22,7 +22,7 @@ namespace butterflow_ui
private const string REGEX_VERSION = @"(?<Major>\d+ ?)\.(?<Minor>\d+ ?)\.(?<Patch>\d+ ?)";
/// <summary> The version status of the current installation. </summary>
private static VersionStatus versionStatus = VersionStatus.unknown;
private static VersionStatus versionStatus = VersionStatus.Unknown;
#endregion
@ -46,13 +46,13 @@ namespace butterflow_ui
{
switch (CurrentVersionStatus)
{
case VersionStatus.current:
case VersionStatus.Current:
return Localization.Localization.CurrentVersionStatusDescription;
case VersionStatus.behind:
case VersionStatus.Behind:
return Localization.Localization.BehindVersionStatusDescription;
case VersionStatus.custom:
case VersionStatus.Custom:
return Localization.Localization.CustomVersionStatusDescription;
case VersionStatus.unknown:
case VersionStatus.Unknown:
default:
return Localization.Localization.UnknownVersionStatusDescription;
}
@ -77,44 +77,53 @@ namespace butterflow_ui
/// <returns> The current version status. </returns>
private static VersionStatus GetVersionStatus()
{
var interpreter = new InputInterpreter();
var client = new GitHubClient(new ProductHeaderValue("butterflow-ui"));
var releases = client.Repository.Release.GetAll("wagesj45", "butterflow-ui").Result;
if (releases.Any())
try
{
var latest = releases.First();
decimal latestMajor = 0, latestMinor = 0, latestPatch = 0, currentMajor = 0, currentMinor = 0, currentPatch = 0;
var interpreter = new InputInterpreter();
var client = new GitHubClient(new ProductHeaderValue("butterflow-ui"));
var releases = client.Repository.Release.GetAll("wagesj45", "butterflow-ui").Result;
var regex = new Regex(REGEX_VERSION);
foreach (Match match in regex.Matches(latest.TagName))
if (releases.Any())
{
latestMajor = interpreter.ComputeExpression(match.Groups["Major"].Value);
latestMinor = interpreter.ComputeExpression(match.Groups["Minor"].Value);
latestPatch = interpreter.ComputeExpression(match.Groups["Patch"].Value);
}
var latest = releases.First();
decimal latestMajor = 0, latestMinor = 0, latestPatch = 0, currentMajor = 0, currentMinor = 0, currentPatch = 0;
foreach (Match match in regex.Matches(Assembly.GetExecutingAssembly().GetName().Version.ToString()))
{
currentMajor = interpreter.ComputeExpression(match.Groups["Major"].Value);
currentMinor = interpreter.ComputeExpression(match.Groups["Minor"].Value);
currentPatch = interpreter.ComputeExpression(match.Groups["Patch"].Value);
}
var regex = new Regex(REGEX_VERSION);
foreach (Match match in regex.Matches(latest.TagName))
{
latestMajor = interpreter.ComputeExpression(match.Groups["Major"].Value);
latestMinor = interpreter.ComputeExpression(match.Groups["Minor"].Value);
latestPatch = interpreter.ComputeExpression(match.Groups["Patch"].Value);
}
if (latestMajor == currentMajor && latestMinor == currentMinor && latestPatch == currentPatch)
{
return VersionStatus.current;
}
foreach (Match match in regex.Matches(Assembly.GetExecutingAssembly().GetName().Version.ToString()))
{
currentMajor = interpreter.ComputeExpression(match.Groups["Major"].Value);
currentMinor = interpreter.ComputeExpression(match.Groups["Minor"].Value);
currentPatch = interpreter.ComputeExpression(match.Groups["Patch"].Value);
}
if (latestMajor >= currentMajor && latestMinor >= currentMinor && latestPatch >= currentPatch)
{
return VersionStatus.behind;
}
if (latestMajor == currentMajor && latestMinor == currentMinor && latestPatch == currentPatch)
{
return VersionStatus.Current;
}
return VersionStatus.custom;
if (latestMajor >= currentMajor && latestMinor >= currentMinor && latestPatch >= currentPatch)
{
return VersionStatus.Behind;
}
return VersionStatus.Custom;
}
}
catch(Exception e)
{
//There was an issue connecting to Github. This could be caused by a missing network connection.
//We can safely ignore an error in this process and proceed, falling through to the default connection
//value of Unknown.
}
return VersionStatus.unknown;
return VersionStatus.Unknown;
}
#endregion
@ -125,13 +134,13 @@ namespace butterflow_ui
public enum VersionStatus
{
/// <summary> The current version is up to date with the github repository. </summary>
current,
Current,
/// <summary> The current version is behind the github repository and should be updated. </summary>
behind,
Behind,
/// <summary> The current version is ahead of the github repository, or is a custom version of butterflow-ui that cannot be compared to the github repository. </summary>
custom,
Custom,
/// <summary> Github failed to respond with the current version. This could be because of rate limits or a network failure. </summary>
unknown
Unknown
}
#endregion

View File

@ -48,6 +48,8 @@
<Reference Include="Octokit, Version=0.31.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\Octokit.0.31.0\lib\net45\Octokit.dll</HintPath>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
@ -60,8 +62,6 @@
<RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference>
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="App.xaml">

View File

@ -2,5 +2,4 @@
<packages>
<package id="csmic" version="1.1.4" targetFramework="net471" />
<package id="Gu.Wpf.Media" version="0.5.0.2" targetFramework="net471" />
<package id="Octokit" version="0.31.0" targetFramework="net471" />
</packages>