Skip to content

Commit c1f4e87

Browse files
committed
CSHARP-2093: updated incompatible server exception message.
1 parent 3eae8f9 commit c1f4e87

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

src/MongoDB.Driver.Core/Core/Clusters/Cluster.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,18 @@ internal abstract class Cluster : ICluster
3939
// static fields
4040
private static readonly TimeSpan __minHeartbeatInterval = TimeSpan.FromMilliseconds(500);
4141
private static readonly Range<int> __supportedWireVersionRange = new Range<int>(2, 6);
42+
private static readonly SemanticVersion __minSupportedServerVersion = new SemanticVersion(2, 6, 0);
4243
private static readonly IServerSelector __randomServerSelector = new RandomServerSelector();
4344

4445
// static properties
46+
/// <summary>
47+
/// Gets the minimum supported server version.
48+
/// </summary>
49+
/// <value>
50+
/// The minimum supported server version
51+
/// </value>
52+
public static SemanticVersion MinSupportedServerVersion => __minSupportedServerVersion;
53+
4554
/// <summary>
4655
/// Gets the supported wire version range.
4756
/// </summary>
@@ -416,7 +425,7 @@ public IServer SelectServer()
416425
}
417426
}
418427

419-
ThrowIfIncompatible(_description);
428+
MongoIncompatibleDriverException.ThrowIfNotSupported(_description);
420429

421430
var connectedServers = _description.Servers.Where(s => s.State == ServerState.Connected);
422431
var selectedServers = _selector.SelectServers(_description, connectedServers).ToList();
@@ -488,16 +497,6 @@ private IServerSelector DecorateSelector(IServerSelector selector)
488497

489498
return selector;
490499
}
491-
private void ThrowIfIncompatible(ClusterDescription description)
492-
{
493-
var isIncompatible = description.Servers
494-
.Any(sd => sd.WireVersionRange != null && !sd.WireVersionRange.Overlaps(__supportedWireVersionRange));
495-
496-
if (isIncompatible)
497-
{
498-
throw new MongoIncompatibleDriverException(description);
499-
}
500-
}
501500
}
502501

503502
private sealed class WaitForDescriptionChangedHelper : IDisposable

src/MongoDB.Driver.Core/MongoIncompatibleDriverException.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-2016 MongoDB Inc.
1+
/* Copyright 2013-2017 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -14,10 +14,12 @@
1414
*/
1515

1616
using System;
17+
using System.Linq;
1718
#if NET45
1819
using System.Runtime.Serialization;
1920
#endif
2021
using MongoDB.Driver.Core.Clusters;
22+
using MongoDB.Driver.Core.Misc;
2123

2224
namespace MongoDB.Driver
2325
{
@@ -31,11 +33,30 @@ public class MongoIncompatibleDriverException : MongoClientException
3133
{
3234
#region static
3335
// static methods
34-
private static string FormatMessage(ClusterDescription clusterDescription)
36+
internal static void ThrowIfNotSupported(ClusterDescription description)
3537
{
36-
return string.Format(
37-
"This version of the driver is not compatible with one or more of the servers to which it is connected: {0}.",
38-
clusterDescription);
38+
var isIncompatible = description.Servers
39+
.Any(sd => sd.WireVersionRange != null && !sd.WireVersionRange.Overlaps(Cluster.SupportedWireVersionRange));
40+
41+
if (isIncompatible)
42+
{
43+
throw new MongoIncompatibleDriverException(description);
44+
}
45+
}
46+
47+
private static string FormatMessage(ClusterDescription description)
48+
{
49+
var incompatibleServer = description.Servers
50+
.FirstOrDefault(sd => sd.WireVersionRange != null && !sd.WireVersionRange.Overlaps(Cluster.SupportedWireVersionRange));
51+
52+
if (incompatibleServer.WireVersionRange.Max < Cluster.SupportedWireVersionRange.Min)
53+
{
54+
return $"Server at {EndPointHelper.ToString(incompatibleServer.EndPoint)} reports wire version {incompatibleServer.WireVersionRange.Max},"
55+
+ $" but this version of the driver requires at least {Cluster.SupportedWireVersionRange.Min} (MongoDB {Cluster.MinSupportedServerVersion}).";
56+
}
57+
58+
return $"Server at {EndPointHelper.ToString(incompatibleServer.EndPoint)} requires wire version {incompatibleServer.WireVersionRange.Min},"
59+
+ $" but this version of the driver only supports up to {Cluster.SupportedWireVersionRange.Max}.";
3960
}
4061
#endregion
4162

tests/MongoDB.Driver.Core.Tests/Core/Clusters/ClusterTests.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -304,15 +304,16 @@ public void SelectServer_should_throw_if_the_matched_server_cannot_be_found_and_
304304
}
305305

306306
[Theory]
307-
[ParameterAttributeData]
308-
public void SelectServer_should_throw_if_any_servers_are_incompatible(
309-
[Values(false, true)]
310-
bool async)
307+
[InlineData(0, 0, false)]
308+
[InlineData(0, 0, true)]
309+
[InlineData(10, 12, false)]
310+
[InlineData(10, 12, true)]
311+
public void SelectServer_should_throw_if_any_servers_are_incompatible(int min, int max, bool async)
311312
{
312313
var subject = CreateSubject();
313314
subject.Initialize();
314315

315-
var connected = ServerDescriptionHelper.Connected(subject.Description.ClusterId, wireVersionRange: new Range<int>(10, 12));
316+
var connected = ServerDescriptionHelper.Connected(subject.Description.ClusterId, wireVersionRange: new Range<int>(min, max));
316317
subject.SetServerDescriptions(connected);
317318
_capturedEvents.Clear();
318319

@@ -328,7 +329,7 @@ public void SelectServer_should_throw_if_any_servers_are_incompatible(
328329
act = () => subject.SelectServer(selector, CancellationToken.None);
329330
}
330331

331-
act.ShouldThrow<MongoException>();
332+
act.ShouldThrow<MongoIncompatibleDriverException>();
332333

333334
_capturedEvents.Next().Should().BeOfType<ClusterSelectingServerEvent>();
334335
_capturedEvents.Next().Should().BeOfType<ClusterSelectingServerFailedEvent>();

0 commit comments

Comments
 (0)