Skip to content

Commit 552e004

Browse files
committed
Add ServerError.TryCreate() method
1 parent 48304e3 commit 552e004

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

src/Elasticsearch.Net/Responses/ServerException/ServerError.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,24 @@ public ServerError(Error error, int? statusCode)
1818
public Error Error { get; }
1919
public int Status { get; }
2020

21+
public static bool TryCreate(Stream stream, out ServerError serverError)
22+
{
23+
try
24+
{
25+
serverError = Create(stream);
26+
return true;
27+
}
28+
catch
29+
{
30+
serverError = null;
31+
return false;
32+
}
33+
}
34+
2135
public static ServerError Create(Stream stream) =>
2236
LowLevelRequestResponseSerializer.Instance.Deserialize<ServerError>(stream);
2337

38+
// TODO: make token default parameter in 7.x
2439
public static Task<ServerError> CreateAsync(Stream stream, CancellationToken token) =>
2540
LowLevelRequestResponseSerializer.Instance.DeserializeAsync<ServerError>(stream, token);
2641

src/Elasticsearch.Net/Responses/Special/BytesResponse.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,11 @@ public BytesResponse() { }
1212
public bool TryGetServerError(out ServerError serverError)
1313
{
1414
serverError = null;
15-
if (this.Body == null || this.Body.Length == 0) return false;
16-
try
17-
{
18-
using(var stream = new MemoryStream(this.Body))
19-
serverError = ServerError.Create(stream);
20-
return true;
21-
}
22-
catch
23-
{
15+
if (this.Body == null || this.Body.Length == 0)
2416
return false;
25-
}
17+
18+
using(var stream = new MemoryStream(this.Body))
19+
return ServerError.TryCreate(stream, out serverError);
2620
}
2721

2822
protected override bool TryGetServerErrorReason(out string reason)

src/Elasticsearch.Net/Responses/Special/StringResponse.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,11 @@ public StringResponse() { }
1212
public bool TryGetServerError(out ServerError serverError)
1313
{
1414
serverError = null;
15-
if (string.IsNullOrEmpty(this.Body)) return false;
16-
try
17-
{
18-
using(var stream = new MemoryStream(Encoding.UTF8.GetBytes(this.Body)))
19-
serverError = ServerError.Create(stream);
20-
return true;
21-
}
22-
catch (Exception)
23-
{
15+
if (string.IsNullOrEmpty(this.Body))
2416
return false;
25-
}
17+
18+
using(var stream = new MemoryStream(Encoding.UTF8.GetBytes(this.Body)))
19+
return ServerError.TryCreate(stream, out serverError);
2620
}
2721

2822
protected override bool TryGetServerErrorReason(out string reason)

0 commit comments

Comments
 (0)