Skip to content

Commit 7fe5e80

Browse files
committed
CSHARP-2294: Added HasErrorLabel and RemoveErrorLabel.
1 parent 0d3d6e4 commit 7fe5e80

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

src/MongoDB.Driver.Core/MongoException.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,31 @@ public MongoException(SerializationInfo info, StreamingContext context)
8383
public void AddErrorLabel(string errorLabel)
8484
{
8585
Ensure.IsNotNull(errorLabel, nameof(errorLabel));
86-
_errorLabels.Add(errorLabel);
86+
if (!_errorLabels.Contains(errorLabel))
87+
{
88+
_errorLabels.Add(errorLabel);
89+
}
90+
}
91+
92+
/// <summary>
93+
/// Determines whether the exception has some error label.
94+
/// </summary>
95+
/// <param name="errorLabel">The error label.</param>
96+
/// <returns>
97+
/// <c>true</c> if the exception has some error label; otherwise, <c>false</c>.
98+
/// </returns>
99+
public bool HasErrorLabel(string errorLabel)
100+
{
101+
return _errorLabels.Contains(errorLabel);
102+
}
103+
104+
/// <summary>
105+
/// Removes the error label.
106+
/// </summary>
107+
/// <param name="errorLabel">The error label.</param>
108+
public void RemoveErrorLabel(string errorLabel)
109+
{
110+
_errorLabels.Remove(errorLabel);
87111
}
88112

89113
#if NET45

tests/MongoDB.Driver.Core.Tests/MongoExceptionTests.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,60 @@ public void AddErrorLabels_should_have_expected_result(
8888
subject.ErrorLabels.Should().Equal(existingErrorLabels.Concat(new[] { newErrorLabel }));
8989
}
9090

91+
[Fact]
92+
public void AddErrorLabels_should_not_add_duplicates()
93+
{
94+
var subject = new MongoException(_message);
95+
96+
subject.AddErrorLabel("one");
97+
subject.AddErrorLabel("one");
98+
99+
subject.ErrorLabels.Should().HaveCount(1);
100+
}
101+
102+
[Theory]
103+
[ParameterAttributeData]
104+
public void HasErrorLabel_should_have_expected_result(
105+
[Values(0, 1, 2, 3)] int existingCount)
106+
{
107+
var subject = new MongoException(_message);
108+
for (var i = 0; i < existingCount; i++)
109+
{
110+
var errorLabel = $"label{i}";
111+
subject.AddErrorLabel(errorLabel);
112+
}
113+
114+
foreach (var errorLabel in subject.ErrorLabels)
115+
{
116+
subject.HasErrorLabel(errorLabel).Should().BeTrue();
117+
}
118+
subject.HasErrorLabel("x").Should().BeFalse();
119+
}
120+
121+
[Theory]
122+
[InlineData(new string[0], "x")]
123+
[InlineData(new[] { "one" }, "one")]
124+
[InlineData(new[] { "one" }, "x")]
125+
[InlineData(new[] { "one", "two" }, "one")]
126+
[InlineData(new[] { "one", "two" }, "two")]
127+
[InlineData(new[] { "one", "two" }, "x")]
128+
[InlineData(new[] { "one", "two", "three" }, "one")]
129+
[InlineData(new[] { "one", "two", "three" }, "two")]
130+
[InlineData(new[] { "one", "two", "three" }, "three")]
131+
[InlineData(new[] { "one", "two", "three" }, "x")]
132+
public void RemoveErrorLabels_should_have_expected_result(string[] errorLabels, string removeErrorLabel)
133+
{
134+
var subject = new MongoException(_message);
135+
foreach (var errorLabel in errorLabels)
136+
{
137+
subject.AddErrorLabel(errorLabel);
138+
}
139+
140+
subject.RemoveErrorLabel(removeErrorLabel);
141+
142+
subject.ErrorLabels.Should().Equal(errorLabels.Where(x => x != removeErrorLabel));
143+
}
144+
91145
#if NET45
92146
[Fact]
93147
public void Serialization_should_work()

0 commit comments

Comments
 (0)