-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Conversation
… 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(); | ||
} |
There was a problem hiding this comment.
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
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: 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
One thing I forgot: 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); | ||
} |
There was a problem hiding this comment.
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);
}
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@thoemmi 👍
If the user.config file is corrupt the whole application 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 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.