Merge pull request #21383 from JamesNK/jamesnk/channelcredentials-improve-error

Improve ChannelCredentials.Create error messsage with non-SslCredentials
pull/21429/head
Jan Tattermusch 5 years ago committed by GitHub
commit a12a8f0ce2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      src/csharp/Grpc.Core.Api/ChannelCredentials.cs
  2. 3
      src/csharp/Grpc.Core.Tests/ChannelCredentialsTest.cs

@ -30,7 +30,7 @@ namespace Grpc.Core
/// </summary>
public abstract class ChannelCredentials
{
static readonly ChannelCredentials InsecureInstance = new InsecureCredentialsImpl();
static readonly ChannelCredentials InsecureInstance = new InsecureCredentials();
/// <summary>
/// Creates a new instance of channel credentials
@ -74,7 +74,7 @@ namespace Grpc.Core
/// </summary>
internal virtual bool IsComposable => false;
private sealed class InsecureCredentialsImpl : ChannelCredentials
private sealed class InsecureCredentials : ChannelCredentials
{
public override void InternalPopulateConfiguration(ChannelCredentialsConfiguratorBase configurator, object state)
{
@ -101,7 +101,11 @@ namespace Grpc.Core
{
this.channelCredentials = GrpcPreconditions.CheckNotNull(channelCredentials);
this.callCredentials = GrpcPreconditions.CheckNotNull(callCredentials);
GrpcPreconditions.CheckArgument(channelCredentials.IsComposable, "Supplied channel credentials do not allow composition.");
if (!channelCredentials.IsComposable)
{
throw new ArgumentException(string.Format("CallCredentials can't be composed with {0}. CallCredentials must be used with secure channel credentials like SslCredentials.", channelCredentials.GetType().Name));
}
}
public override void InternalPopulateConfiguration(ChannelCredentialsConfiguratorBase configurator, object state)

@ -40,7 +40,8 @@ namespace Grpc.Core.Tests
Assert.Throws(typeof(ArgumentNullException), () => ChannelCredentials.Create(new FakeChannelCredentials(true), null));
// forbid composing non-composable
Assert.Throws(typeof(ArgumentException), () => ChannelCredentials.Create(new FakeChannelCredentials(false), new FakeCallCredentials()));
var ex = Assert.Throws(typeof(ArgumentException), () => ChannelCredentials.Create(new FakeChannelCredentials(false), new FakeCallCredentials()));
Assert.AreEqual("CallCredentials can't be composed with FakeChannelCredentials. CallCredentials must be used with secure channel credentials like SslCredentials.", ex.Message);
}
[Test]

Loading…
Cancel
Save