Skip to content

Commit

Permalink
[PORT #6167] Improve error/exception message for misconfigured cluste…
Browse files Browse the repository at this point in the history
…r provider (#6169)

* Improve error/exception message for misconfigured cluster provider (#6167)

* Improve error/exception message for misconfigured cluster provider

* Remove pattern matching

* Improve validation to also catch other invalid configuration scenario

* Simplify code

* Update API Verify list

(cherry picked from commit 5f8710d)

* Update API Verify list
  • Loading branch information
Arkatufus committed Oct 12, 2022
1 parent d82cdc2 commit eba7bf5
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 9 deletions.
@@ -0,0 +1,26 @@
// -----------------------------------------------------------------------
// <copyright file="InvalidSettingsSpec.cs" company="Akka.NET Project">
// Copyright (C) 2009-2022 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2022 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
// -----------------------------------------------------------------------

using Akka.Configuration;
using Akka.TestKit;
using FluentAssertions;
using Xunit;
using static FluentAssertions.FluentActions;

namespace Akka.Cluster.Sharding.Tests
{
public class InvalidSettingsSpec: AkkaSpec
{
[Fact(DisplayName = "ClusterSharding started with invalid actor provider should raise a user friendly exception")]
public void InvalidActorProviderTest()
{
Invoking(() => ClusterSharding.Get(Sys)) // throws here
.Should().ThrowExactly<ConfigurationException>()
.WithMessage("*Did you forgot*");
}
}
}
Expand Up @@ -2206,7 +2206,7 @@ namespace Akka.Configuration
public ConfigurationException(string message) { }
public ConfigurationException(string message, System.Exception exception) { }
protected ConfigurationException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
public static Akka.Configuration.ConfigurationException NullOrEmptyConfig<T>(string path = null) { }
public static Akka.Configuration.ConfigurationException NullOrEmptyConfig<T>(string path = null, string reason = null) { }
}
public class ConfigurationFactory
{
Expand Down
Expand Up @@ -2208,7 +2208,7 @@ namespace Akka.Configuration
public ConfigurationException(string message) { }
public ConfigurationException(string message, System.Exception exception) { }
protected ConfigurationException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
public static Akka.Configuration.ConfigurationException NullOrEmptyConfig<T>(string path = null) { }
public static Akka.Configuration.ConfigurationException NullOrEmptyConfig<T>(string path = null, string reason = null) { }
}
public class ConfigurationFactory
{
Expand Down
Expand Up @@ -2206,7 +2206,7 @@ namespace Akka.Configuration
public ConfigurationException(string message) { }
public ConfigurationException(string message, System.Exception exception) { }
protected ConfigurationException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
public static Akka.Configuration.ConfigurationException NullOrEmptyConfig<T>(string path = null) { }
public static Akka.Configuration.ConfigurationException NullOrEmptyConfig<T>(string path = null, string reason = null) { }
}
public class ConfigurationFactory
{
Expand Down
Expand Up @@ -2239,7 +2239,7 @@ namespace Akka.Configuration
public ConfigurationException(string message) { }
public ConfigurationException(string message, System.Exception exception) { }
protected ConfigurationException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
public static Akka.Configuration.ConfigurationException NullOrEmptyConfig<T>(string path = null) { }
public static Akka.Configuration.ConfigurationException NullOrEmptyConfig<T>(string path = null, string reason = null) { }
}
public class ConfigurationFactory
{
Expand Down
31 changes: 31 additions & 0 deletions src/core/Akka.Cluster.Tests/InvalidClusterSettingsSpec.cs
@@ -0,0 +1,31 @@
// -----------------------------------------------------------------------
// <copyright file="InvalidClusterSettingsSpec.cs" company="Akka.NET Project">
// Copyright (C) 2009-2022 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2022 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
// -----------------------------------------------------------------------

using Akka.Configuration;
using Akka.TestKit;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;
using static FluentAssertions.FluentActions;

namespace Akka.Cluster.Tests
{
public class InvalidClusterSettingsSpec : AkkaSpec
{
public InvalidClusterSettingsSpec(ITestOutputHelper output) : base(output)
{
}

[Fact(DisplayName = "Cluster started with invalid actor provider should raise a user friendly exception")]
public void InvalidActorProviderTest()
{
Invoking(() => Cluster.Get(Sys)) // throws here
.Should().ThrowExactly<ConfigurationException>()
.WithMessage("*Did you forgot*");
}
}
}
4 changes: 2 additions & 2 deletions src/core/Akka.Cluster/ClusterSettings.cs
Expand Up @@ -34,8 +34,8 @@ public ClusterSettings(Config config, string systemName)
{
//TODO: Requiring!
var clusterConfig = config.GetConfig("akka.cluster");
if (clusterConfig.IsNullOrEmpty())
throw ConfigurationException.NullOrEmptyConfig<ClusterSettings>("akka.cluster");
if (clusterConfig?.GetConfig("failure-detector") == null)
throw ConfigurationException.NullOrEmptyConfig<ClusterSettings>("akka.cluster", "Did you forgot to set the 'akka.cluster.provider' HOCON property to 'cluster'?");

LogInfoVerbose = clusterConfig.GetBoolean("log-info-verbose", false);
LogInfo = LogInfoVerbose || clusterConfig.GetBoolean("log-info", false);
Expand Down
6 changes: 3 additions & 3 deletions src/core/Akka/Configuration/ConfigurationException.cs
Expand Up @@ -16,11 +16,11 @@ namespace Akka.Configuration
/// </summary>
public class ConfigurationException : AkkaException
{
public static ConfigurationException NullOrEmptyConfig<T>(string path = null)
public static ConfigurationException NullOrEmptyConfig<T>(string path = null, string reason = null)
{
if (!string.IsNullOrWhiteSpace(path))
return new ConfigurationException($"Failed to instantiate {typeof(T).Name}: Configuration does not contain `{path}` node");
return new ConfigurationException($"Failed to instantiate {typeof(T).Name}: Configuration is null or empty.");
return new ConfigurationException($"Failed to instantiate {typeof(T).Name}: Configuration does not contain `{path}` node{(reason != null ? $". {reason}" : "")}");
return new ConfigurationException($"Failed to instantiate {typeof(T).Name}: Configuration is null or empty{(reason != null ? $". {reason}" : "")}");
}

/// <summary>
Expand Down

0 comments on commit eba7bf5

Please sign in to comment.