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

Conversation

Remolutionary
Copy link

I had the problem, that a customer could suddenly not open my
application anymore. It turned out that this is because the user.config
file was just empty and this caused a ConfigurationErrorsException. Then I
added an UnhandledExceptionHandler but this exception seems to happen
earlier. The only working solution is to delete the odd user.config and
restart the application.
For more details see here:
http://www.codeproject.com/Articles/30216/Handling-Corrupt-user-config-Settings

You can test this by editing the user.config and delete all content. But do NOT delete the file!
Then you need a window with the setting SaveWindowPosition="True" - otherwise no user.config file is created.

… crashes

I had the problem, that a customer could suddenly not open my
application anymore. It turned out that this is because the user.config
file was just empty and this caused a ConfigurationException. Then I
added an UnhandledExceptionHandler but this exception seems to happen
earlier. The only working solution ist to delete the odd user.config and
restart the application.
For more details see here:

http://www.codeproject.com/Articles/30216/Handling-Corrupt-user-config-Settings
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

@Remolutionary
Copy link
Author

An exception is already thrown. The isssue has been that I could not catch it in my application but it turned out that I defined my UnhandledExepctionHandler too late. I'm using NLOG as log System and the ConfigurationErrorException is thrown when a logger is defined:
public App()
{
_logger = LogManager.GetLogger("App");
}
If I add a trycatch to this line I can catch the error although the logger does not cause the problem. In fact it is the settings-system which loads the settings as soon as the app starts.

So I think my pull request is in fact needless...sorry.

The ConfigurationErrorsException can't be catched by an
UnhandledExceptionHandler. So it is better to wrap it into a
MahAppsException
@Remolutionary Remolutionary reopened this Sep 4, 2015
@Remolutionary
Copy link
Author

One thing I forgot:
The ConfigurationErrorsException can't be catched by an UnhandledExceptionHandler. So it is necessary to wrap it into a MahAppsException. Otherwise the user can't handle the situation because he does not know when this error arises.

In my special case with the logger I can catch the exception now but this is more accidentally I would say. If I remove this line of code I have no chance to catch this kind of exception.

So I reopen my pull request.

catch (ConfigurationErrorsException ex)
{
var filename = ((ConfigurationErrorsException)ex.InnerException).Filename;
throw new MahAppsException(string.Format("The settings file {0} seems to be corrupted", filename), ex);
}
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 👍

punker76 added a commit that referenced this pull request Sep 5, 2015
If the user.config file is corrupt the whole application crashes
@punker76 punker76 merged commit f0f5479 into MahApps:master Sep 5, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants