Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If the user.config file is corrupt the whole application crashes #2112

Merged
merged 3 commits into from
Sep 5, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions MahApps.Metro/Controls/WindowSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
Expand Down Expand Up @@ -64,9 +65,20 @@ public bool UpgradeSettings
{
get
{
if (this["UpgradeSettings"] != null)
try
{
return (bool)this["UpgradeSettings"];
if (this["UpgradeSettings"] != null)
{
return (bool)this["UpgradeSettings"];
}
}
catch (Exception ex)
{
Debug.WriteLine("Failed to load settings file:\r\n{0}", ex);
var filename = ((ConfigurationErrorsException)ex.InnerException).Filename;
File.Delete(filename);
Process.Start(Application.ResourceAssembly.Location);
Application.Current.Shutdown();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think MahApps should not delete any stuff. instead we should clearly say what is going wrong. so you can use the new MahAppsException, so you can handle this in your application and tell the user, that he must delete their config file.

MahAppsException -> https://github.com/MahApps/MahApps.Metro/blob/master/MahApps.Metro/MahAppsException.cs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for my nitpicking ;-) but why must the inner exception cast to ConfigurationErrorsException? the ex is already a ConfigurationErrorsException?

should this better so?

catch (ConfigurationErrorsException ex)
{
    var filename = ex.Filename;
    throw new MahAppsException(string.Format("The settings file {0} seems to be corrupted", filename), ex.InnerException);
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes they are the same exception type but in the outer exception Filename is null. You really have to go to the InnerException and cast it… I just debugged the code again to be sure about that.

Von: Jan Karger [mailto:notifications@github.com]
Gesendet: Freitag, 4. September 2015 21:24
An: MahApps/MahApps.Metro MahApps.Metro@noreply.github.com
Cc: Markus markus.remmel@remolution-software.com
Betreff: Re: [MahApps.Metro] If the user.config file is corrupt the whole application crashes (#2112)

In MahApps.Metro/Controls/WindowSettings.cs #2112 (comment) :

             {
  •                return (bool)this["UpgradeSettings"];
    
  •                if (this["UpgradeSettings"] != null)
    
  •                {
    
  •                    return (bool)this["UpgradeSettings"];
    
  •                }
    
  •            }
    
  •            catch (ConfigurationErrorsException ex)
    
  •            {
    
  •                var filename = ((ConfigurationErrorsException)ex.InnerException).Filename;
    
  •                throw new MahAppsException(string.Format("The settings file {0} seems to be corrupted", filename), ex);
             }
    

sorry for my nitpicking ;-) but why must the inner exception cast to ConfigurationErrorsException? the ex is already a ConfigurationErrorsException?

should this better so?

catch (ConfigurationErrorsException ex)
{
var filename = ex.Filename;
throw new MahAppsException(string.Format("The settings file {0} seems to be corrupted", filename), ex.InnerException);
}


Reply to this email directly or view it on GitHub https://github.com/MahApps/MahApps.Metro/pull/2112/files#r38785880 . https://github.com/notifications/beacon/AD73r6m86DZcFr3Smn9bz9rsLANCtqnRks5ouedigaJpZM4F3__-.gif

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Remolutionary ok, really don't know this, so i think this could be a final solution

catch (ConfigurationErrorsException ex)
{
    var innerEx = ex.InnerException as ConfigurationErrorsException;
    var filename = innerEx != null ? innerError.Filename : "<unknown>";
    throw new MahAppsException(string.Format("The settings file '{0}' seems to be corrupted", filename), ex.InnerException);
}

thx

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer more robust code like this:

catch (ConfigurationErrorsException ex)
{
    string filename = null;
    while (ex != null && (filename = ex.Filename) == null)
    {
        ex = ex.InnerException as ConfigurationErrorsException;
    }
    throw new MahAppsException(string.Format("The settings file '{0}' seems to be corrupted", filename ?? "<unknown>"), ex);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thoemmi 👍

return true;
}
Expand Down
1 change: 1 addition & 0 deletions MahApps.Metro/MahApps.Metro.NET45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Windows.Interactivity, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Lib\NET45\System.Windows.Interactivity.dll</HintPath>
Expand Down
1 change: 1 addition & 0 deletions MahApps.Metro/MahApps.Metro.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Windows.Interactivity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Lib\NET40\System.Windows.Interactivity.dll</HintPath>
Expand Down
1 change: 1 addition & 0 deletions samples/MetroDemo/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
d:DataContext="{d:DesignInstance MetroDemo:MainWindowViewModel}"
Closing="MetroWindow_Closing"
Dialog:DialogParticipation.Register="{Binding}"
SaveWindowPosition="True"
>

<Window.Resources>
Expand Down