Skip to content

Commit 1a17ae2

Browse files
committed
ElasticsearchServerException's message now goes into .Message on base Exception class
1 parent 277dc01 commit 1a17ae2

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

src/Elasticsearch.Net/Connection/ElasticsearchServerException.cs

+17-11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Linq;
55
using System.Net;
6+
using System.Runtime.InteropServices;
67
using System.Text;
78
using System.Text.RegularExpressions;
89

@@ -12,28 +13,33 @@ public class ElasticsearchServerError
1213
{
1314
public int Status { get; set; }
1415
public string Error { get; set; }
16+
public string ExceptionType { get; set; }
1517

1618
}
1719

1820
public class ElasticsearchServerException : Exception
1921
{
20-
private static Regex ExceptionSplitter = new Regex(@"^([^\[]*?)\[(.*)\]", RegexOptions.Singleline);
22+
private static readonly Regex ExceptionSplitter = new Regex(@"^([^\[]*?)\[(.*)\]", RegexOptions.Singleline);
23+
private static readonly string _couldNotParseServerException = "Could not parse server exception";
2124

2225
public int Status { get; set; }
23-
public string Error { get; set; }
2426
public string ExceptionType { get; set; }
25-
public ElasticsearchServerException(ElasticsearchServerError error)
27+
public ElasticsearchServerException(ElasticsearchServerError error) : base(ParseError(error))
2628
{
27-
if (error == null) return;
2829
this.Status = error.Status;
29-
if (error.Error.IsNullOrEmpty()) return;
30+
this.ExceptionType = error.ExceptionType;
31+
}
32+
//iffy side effect assignment to exceptionType needed so that we simply return message to the
33+
//base constructor.
34+
private static string ParseError(ElasticsearchServerError error)
35+
{
36+
if (error == null) return _couldNotParseServerException;
37+
if (error.Error.IsNullOrEmpty()) return _couldNotParseServerException;
3038
var matches = ExceptionSplitter.Match(error.Error);
31-
if (matches.Groups.Count == 3)
32-
{
33-
this.Error = matches.Groups[2].Value;
34-
this.ExceptionType = matches.Groups[1].Value;
35-
}
36-
else this.Error = error.Error;
39+
if (matches.Groups.Count != 3) return _couldNotParseServerException;
40+
41+
error.ExceptionType = matches.Groups[1].Value;
42+
return matches.Groups[2].Value;
3743
}
3844
}
3945
}

src/Tests/Nest.Tests.Integration/RawCalls/HasUsefultServerExceptionTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public void MaxRetryException_DoesNotHide_ElasticsearchServerException()
1818

1919
e.Status.Should().Be(400);
2020
e.ExceptionType.Should().Be("SearchPhaseExecutionException");
21-
e.Error.Should().Contain("Failed to parse source");
21+
e.Message.Should().Contain("Failed to parse source");
2222
}
2323

2424
[Test]
@@ -30,7 +30,7 @@ public void ServerExceptionMakesItOnResponse_EvenIfClientIsNotConfiguredToThrow(
3030

3131
e.Status.Should().Be(400);
3232
e.ExceptionType.Should().Be("SearchPhaseExecutionException");
33-
e.Error.Should().Contain("Failed to parse source");
33+
e.Message.Should().Contain("Failed to parse source");
3434
}
3535
}
3636
}

0 commit comments

Comments
 (0)