Skip to content

Commit

Permalink
Do not print stack trace when invalid configuration is encountered
Browse files Browse the repository at this point in the history
This improves invalid configuration output and makes it easier to spot
what the actual issue is.
This also ensures that an invalid configuration is seen as a graceful
shutdown rather than the server terminating unexpectedly.
  • Loading branch information
pgermishuys committed Jun 30, 2020
1 parent b3e6053 commit 630ddc5
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 35 deletions.
10 changes: 5 additions & 5 deletions src/EventStore.ClusterNode/ClusterVNodeHostedService.cs
Expand Up @@ -151,12 +151,12 @@ internal class ClusterVNodeHostedService : EventStoreHostedService<ClusterNodeOp

_dbLock = new ExclusiveDbLock(absolutePath);
if (!_dbLock.Acquire())
throw new Exception($"Couldn't acquire exclusive lock on DB at '{dbPath}'.");
throw new InvalidConfigurationException($"Couldn't acquire exclusive lock on DB at '{dbPath}'.");
}

_clusterNodeMutex = new ClusterNodeMutex();
if (!_clusterNodeMutex.Acquire())
throw new Exception($"Couldn't acquire exclusive Cluster Node mutex '{_clusterNodeMutex.MutexName}'.");
throw new InvalidConfigurationException($"Couldn't acquire exclusive Cluster Node mutex '{_clusterNodeMutex.MutexName}'.");

if (!opts.DiscoverViaDns && opts.GossipSeed.Length == 0) {
if (opts.ClusterSize == 1) {
Expand Down Expand Up @@ -216,7 +216,7 @@ internal class ClusterVNodeHostedService : EventStoreHostedService<ClusterNodeOp
Log.Information("Quorum size set to {quorum}", prepareCount);

if (options.ReadOnlyReplica && options.ClusterSize <= 1) {
throw new Exception(
throw new InvalidConfigurationException(
"This node cannot be configured as a Read Only Replica as these node types are only supported in a clustered configuration.");
}

Expand Down Expand Up @@ -368,7 +368,7 @@ internal class ClusterVNodeHostedService : EventStoreHostedService<ClusterNodeOp
options.CertificatePrivateKeyFile,
options.CertificatePassword);
} else if (!options.Dev)
throw new Exception("A TLS Certificate is required unless development mode (--dev) is set.");
throw new InvalidConfigurationException("A TLS Certificate is required unless development mode (--dev) is set.");

var authorizationConfig = String.IsNullOrEmpty(options.AuthorizationConfig)
? options.Config
Expand All @@ -377,7 +377,7 @@ internal class ClusterVNodeHostedService : EventStoreHostedService<ClusterNodeOp
if (!string.IsNullOrEmpty(options.TrustedRootCertificatesPath)) {
builder.WithTrustedRootCertificatesPath(options.TrustedRootCertificatesPath);
} else {
throw new Exception(
throw new InvalidConfigurationException(
$"{nameof(options.TrustedRootCertificatesPath)} must be specified unless development mode (--dev) is set.");
}

Expand Down
16 changes: 8 additions & 8 deletions src/EventStore.ClusterNode/EventStore.ClusterNode.csproj
Expand Up @@ -9,15 +9,15 @@
<Platforms>x64</Platforms>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.ComponentModel.Composition" Version="4.7.0"/>
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1"/>
<PackageReference Include="System.ComponentModel.Composition" Version="4.7.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\EventStore.ClusterNode.Web\EventStore.ClusterNode.Web.csproj"/>
<ProjectReference Include="..\EventStore.Common\EventStore.Common.csproj"/>
<ProjectReference Include="..\EventStore.Core\EventStore.Core.csproj"/>
<ProjectReference Include="..\EventStore.Projections.Core\EventStore.Projections.Core.csproj"/>
<ProjectReference Include="..\EventStore.Rags\EventStore.Rags.csproj"/>
<ProjectReference Include="..\EventStore.ClusterNode.Web\EventStore.ClusterNode.Web.csproj" />
<ProjectReference Include="..\EventStore.Common\EventStore.Common.csproj" />
<ProjectReference Include="..\EventStore.Core\EventStore.Core.csproj" />
<ProjectReference Include="..\EventStore.Projections.Core\EventStore.Projections.Core.csproj" />
<ProjectReference Include="..\EventStore.Rags\EventStore.Rags.csproj" />
</ItemGroup>
<ItemGroup>
<None Include="..\EventStore.Common\Log\logconfig.json">
Expand All @@ -26,7 +26,7 @@
</None>
</ItemGroup>
<ItemGroup>
<Content Include="app2.ico"/>
<Content Include="app2.ico" />
</ItemGroup>
<!-- TODO(jen20): Investigate which of these are correct -->
<PropertyGroup>
Expand Down
32 changes: 16 additions & 16 deletions src/EventStore.Common/EventStore.Common.csproj
Expand Up @@ -14,24 +14,24 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.3"/>
<PackageReference Include="Microsoft.Extensions.FileProviders.Composite" Version="3.1.3"/>
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="3.1.3"/>
<PackageReference Include="Newtonsoft.Json" Version="11.0.2"/>
<PackageReference Include="Serilog" Version="2.9.0"/>
<PackageReference Include="Serilog.Enrichers.Process" Version="2.0.1"/>
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0"/>
<PackageReference Include="Serilog.Filters.Expressions" Version="2.1.0"/>
<PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0"/>
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0"/>
<PackageReference Include="Serilog.Sinks.Async" Version="1.4.0"/>
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1"/>
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0"/>
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Composite" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="3.1.3" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="Serilog" Version="2.9.0" />
<PackageReference Include="Serilog.Enrichers.Process" Version="2.0.1" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
<PackageReference Include="Serilog.Filters.Expressions" Version="2.1.0" />
<PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Async" Version="1.4.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\EventStore.Common.Utils\EventStore.Common.Utils.csproj"/>
<ProjectReference Include="..\EventStore.Rags\EventStore.Rags.csproj"/>
<ProjectReference Include="..\EventStore.Common.Utils\EventStore.Common.Utils.csproj" />
<ProjectReference Include="..\EventStore.Rags\EventStore.Rags.csproj" />
</ItemGroup>

</Project>
@@ -0,0 +1,7 @@
using System;

namespace EventStore.Common.Exceptions {
public class InvalidConfigurationException : Exception {
public InvalidConfigurationException(string message) : base(message) { }
}
}
15 changes: 9 additions & 6 deletions src/EventStore.Core/EventStoreHostedService.cs
Expand Up @@ -43,12 +43,15 @@ public abstract class EventStoreHostedService<TOptions> : IHostedService where T
Create(Options);
}
} catch (OptionException exc) {
Console.Error.WriteLine("Error while parsing options:");
Console.Error.WriteLine(FormatExceptionMessage(exc));
Console.Error.WriteLine();
Console.Error.WriteLine("Options:");
Console.Error.WriteLine(EventStoreOptions.GetUsage<TOptions>());
throw;
Log.Error("Error while parsing options:");
Log.Error(FormatExceptionMessage(exc));
Log.Information("Options:");
Log.Information(EventStoreOptions.GetUsage<TOptions>());
_skipRun = true;
} catch (InvalidConfigurationException exc) {
Log.Error("Invalid Configuration Encountered");
Log.Error(exc.Message);
_skipRun = true;
}
}

Expand Down

0 comments on commit 630ddc5

Please sign in to comment.