Skip to content

Commit

Permalink
Show error message if a config file is incorrect (#770)
Browse files Browse the repository at this point in the history
* Give error message if the application does not start due to invalid config file
  • Loading branch information
ErikMogensen authored Feb 25, 2024
1 parent 21df107 commit 979cee5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/ServiceBusExplorer/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<add key="messageDeferProvider" value="ServiceBusExplorer.Helpers.InMemoryMessageDeferProvider,ServiceBusExplorer.Common" />
<add key="Microsoft.ServiceBus.X509RevocationMode" value="NoCheck" />
<add key="ClientSettingsProvider.ServiceUri" value="" />
</appSettings>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
</startup>
Expand Down
8 changes: 7 additions & 1 deletion src/ServiceBusExplorer/Forms/ConnectForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -593,15 +593,19 @@ private void btnSave_Click(object sender, EventArgs e)
{
var key = cboServiceBusNamespace.Text;
var isNewServiceBusNamespace = (key == EnterConnectionString);

ServiceBusConnectionStringBuilder serviceBusConnectionStringBuilder;

try
{
BuildCurrentConnectionString();

if (string.IsNullOrWhiteSpace(ConnectionString))
{
MainForm.StaticWriteToLog(ConnectionStringCannotBeNull);
return;
}

serviceBusConnectionStringBuilder = new ServiceBusConnectionStringBuilder(ConnectionString);
}
catch (Exception)
Expand All @@ -621,10 +625,10 @@ private void btnSave_Click(object sender, EventArgs e)

var index = host.IndexOf(".", StringComparison.Ordinal);


if (isNewServiceBusNamespace)
{
key = index > 0 ? CultureInfo.CurrentCulture.TextInfo.ToTitleCase(host.Substring(0, index)) : "MyNamespace";

using (var parameterForm = new ParameterForm("Enter the key for the Service Bus namespace",
new List<string> { "Key" },
new List<string> { key },
Expand All @@ -644,6 +648,7 @@ private void btnSave_Click(object sender, EventArgs e)
MainForm.StaticWriteToLog("The key of the Service Bus namespace cannot be null.");
return;
}

var value = ConnectionString;

try
Expand All @@ -663,6 +668,7 @@ private void btnSave_Click(object sender, EventArgs e)
}

serviceBusHelper.ServiceBusNamespaces[key] = ServiceBusNamespace.GetServiceBusNamespace(key, value, MainForm.StaticWriteToLog);

cboServiceBusNamespace.Items.Clear();
cboServiceBusNamespace.Items.Add(SelectServiceBusNamespace);
cboServiceBusNamespace.Items.Add(EnterConnectionString);
Expand Down
9 changes: 8 additions & 1 deletion src/ServiceBusExplorer/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4041,7 +4041,14 @@ private void SetTitle(string prefix)
#region Public Static Methods
public static void StaticWriteToLog(string message, bool async = true)
{
mainSingletonMainForm.WriteToLog(message);
if(mainSingletonMainForm != null)
{
mainSingletonMainForm.WriteToLog(message);
}
else
{
MessageBox.Show(message);
}
}
#endregion

Expand Down
11 changes: 10 additions & 1 deletion src/ServiceBusExplorer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,16 @@ static void HandleException(Exception ex)
{
if (ex != null && !string.IsNullOrWhiteSpace(ex.Message))
{
MainForm.StaticWriteToLog(string.Format(CultureInfo.CurrentCulture, ExceptionFormat, ex.Message));
var message = ex.Message;

if (ex.GetType() == typeof(TypeInitializationException))
{
message += Environment.NewLine + Environment.NewLine +
"This may be due to an invalid configuration file.";
}

MainForm.StaticWriteToLog(string.Format(CultureInfo.CurrentCulture, ExceptionFormat, message));

if (ex.InnerException != null && !string.IsNullOrWhiteSpace(ex.InnerException.Message))
{
MainForm.StaticWriteToLog(string.Format(CultureInfo.CurrentCulture, InnerExceptionFormat, ex.InnerException.Message));
Expand Down

0 comments on commit 979cee5

Please sign in to comment.