public void TestConfigurationException() { Exception ex = new ConfigurationException( Message, new Exception(InnerException)); // Save the full ToString() value, including the exception message and stack trace. string exceptionToString = ex.ToString(); // "Save" object state string serializedData = JsonSerializer.Serialize(ex); // Replace the original exception with de-serialized one ex = JsonSerializer.Deserialize <ConfigurationException>(serializedData); // Double-check that the exception message and stack trace (owned by the base Exception) are preserved Assert.Equal(exceptionToString, ex.ToString()); }
public void when_calling_toString_with_configurationFile() { var sut = new ConfigurationException("test") { ConfigurationFile = "myConfigfile" }; var toStr = sut.ToString(); StringAssert.Contains(toStr, "test"); StringAssert.Contains(toStr, "myConfigfile"); }
public void when_calling_toString() { List <Exception> inners = new List <Exception>(); inners.Add(new ArgumentException("Msg1")); inners.Add(new InvalidOperationException("Inv2")); inners.Add(new XmlSchemaValidationException("Validation", new Exception("inner"), 111, 222)); var sut = new ConfigurationException(inners); var toStr = sut.ToString(); StringAssert.Contains(toStr, "Msg1"); StringAssert.Contains(toStr, "Inv2"); StringAssert.Contains(toStr, "Validation"); StringAssert.Contains(toStr, "inner"); StringAssert.Contains(toStr, "111"); StringAssert.Contains(toStr, "222"); }
public void when_serializing_with_inner_shoulld_deserialize_back() { List <Exception> inners = new List <Exception>(); inners.Add(new ArgumentException("Msg1")); inners.Add(new InvalidOperationException("Inv2")); inners.Add(new XmlSchemaValidationException("Validation", new Exception("inner"), 111, 222)); var sut = new ConfigurationException(inners); var str = sut.ToString(); BinaryFormatter formatter = new BinaryFormatter(); using (MemoryStream stream = new MemoryStream()) { formatter.Serialize(stream, sut); stream.Position = 0; var deserialized = formatter.Deserialize(stream) as ConfigurationException; Assert.AreEqual(str, deserialized.ToString()); } }
/// <summary> /// Used in the rare event that the configuration file for the application is corrupted /// </summary> private static void HandleConfigurationException(ConfigurationException ce) { bool exceptionHandled = false; try { // perhaps this should be checked for if it is null var in3 = ce.InnerException.InnerException; // saves having to have a reference to System.Xml just to check that we have an XmlException if (in3.GetType().Name == "XmlException") { var localSettingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "GitExtensions"); // assume that if we are having this error and the installation is not a portable one then the folder will exist. if (Directory.Exists(localSettingsPath)) { string messageContent = string.Format("There is a problem with the user.xml configuration file.{0}{0}The error message was: {1}{0}{0}The configuration file is usually found in: {2}{0}{0}Problems with configuration can usually be solved by deleting the configuration file. Would you like to delete the file?", Environment.NewLine, in3.Message, localSettingsPath); if (DialogResult.Yes.Equals(MessageBox.Show(messageContent, "Configuration Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2))) { try { Directory.Delete(localSettingsPath, true); // deletes all application settings not just for this instance - but should work // Restart GitExtensions with the same arguments after old config is deleted? if (DialogResult.OK.Equals(MessageBox.Show(string.Format("Files have been deleted.{0}{0}Would you like to attempt to restart GitExtensions?", Environment.NewLine), "Configuration Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Information))) { var args = Environment.GetCommandLineArgs(); var p = new System.Diagnostics.Process { StartInfo = { FileName = args[0] } }; if (args.Length > 1) { args[0] = ""; p.StartInfo.Arguments = string.Join(" ", args); } p.Start(); } } catch (IOException) { MessageBox.Show(string.Format("Could not delete all files and folders in {0}!", localSettingsPath), "Configuration Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } // assuming that there is no localSettingsPath directory in existence we probably have a portable installation. else { string messageContent = string.Format("There is a problem with the application settings XML configuration file.{0}{0}The error message was: {1}{0}{0}Problems with configuration can usually be solved by deleting the configuration file.", Environment.NewLine, in3.Message); MessageBox.Show(messageContent, "Configuration Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } exceptionHandled = true; } } finally { // if we fail in this somehow at least this message might get somewhere if (!exceptionHandled) { MessageBox.Show(ce.ToString(), "Configuration Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } Environment.Exit(1); } }