Skip to content

Commit 76da7e5

Browse files
committed
Build DebugInformation uri and method from Request or ApiCallDetails (#4077)
* Build DebugInformation uri and method from Request or ApiCallDetails This commit changes how DebugInformation is built in ElasticsearchClientException to use either Request or ApiCallDetails to retrieve the Uri and method of the request. There are some usages of ElasticsearchClientException where the Request property is not set, such as Throw() method in BulkAllObservable. Fixes #3687
1 parent 98ceaf6 commit 76da7e5

File tree

4 files changed

+39
-17
lines changed

4 files changed

+39
-17
lines changed

src/Elasticsearch.Net/Exceptions/ElasticsearchClientException.cs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Text;
55
using Elasticsearch.Net.Extensions;
6+
using static Elasticsearch.Net.ResponseStatics;
67

78
namespace Elasticsearch.Net
89
{
@@ -31,26 +32,46 @@ public string DebugInformation
3132
var failureReason = FailureReason.GetStringValue();
3233
if (FailureReason == PipelineFailure.Unexpected && AuditTrail.HasAny())
3334
failureReason = "Unrecoverable/Unexpected " + AuditTrail.Last().Event.GetStringValue();
34-
var path = Request.Uri != null
35-
? Request.Uri.ToString()
36-
: Request.PathAndQuery + " on an empty node, likely a node predicate on ConnectionSettings not matching ANY nodes";
3735

38-
sb.AppendLine($"# FailureReason: {failureReason} while attempting {EnumExtensions.GetStringValue(Request.Method)} on {path}");
36+
sb.Append("# FailureReason: ")
37+
.Append(failureReason)
38+
.Append(" while attempting ");
39+
40+
if (Request != null)
41+
{
42+
sb.Append(Request.Method.GetStringValue()).Append(" on ");
43+
if (Request.Uri != null)
44+
sb.AppendLine(Request.Uri.ToString());
45+
else
46+
sb.Append(Request.PathAndQuery)
47+
.AppendLine(" on an empty node, likely a node predicate on ConnectionSettings not matching ANY nodes");
48+
}
49+
else if (Response != null)
50+
{
51+
sb.Append(Response.HttpMethod.GetStringValue())
52+
.Append(" on ")
53+
.AppendLine(Response.Uri.ToString());
54+
}
55+
else
56+
sb.AppendLine("a request");
57+
3958
if (Response != null)
40-
ResponseStatics.DebugInformationBuilder(Response, sb);
59+
DebugInformationBuilder(Response, sb);
4160
else
4261
{
43-
ResponseStatics.DebugAuditTrail(AuditTrail, sb);
44-
ResponseStatics.DebugAuditTrailExceptions(AuditTrail, sb);
62+
DebugAuditTrail(AuditTrail, sb);
63+
DebugAuditTrailExceptions(AuditTrail, sb);
4564
}
65+
4666
if (InnerException != null)
4767
{
48-
sb.AppendLine($"# Inner Exception: {InnerException.Message}");
49-
sb.AppendLine(InnerException.ToString());
68+
sb.Append("# Inner Exception: ")
69+
.AppendLine(InnerException.Message)
70+
.AppendLine(InnerException.ToString());
5071
}
51-
sb.AppendLine($"# Exception:");
52-
sb.AppendLine(ToString());
5372

73+
sb.AppendLine("# Exception:")
74+
.AppendLine(ToString());
5475
return sb.ToString();
5576
}
5677
}

src/Elasticsearch.Net/Responses/ResponseStatics.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public static string DebugInformationBuilder(IApiCallDetails r, StringBuilder sb
3838

3939
public static void DebugAuditTrailExceptions(List<Audit> auditTrail, StringBuilder sb)
4040
{
41+
if (auditTrail == null) return;
42+
4143
var auditExceptions = auditTrail.Select((audit, i) => new { audit, i }).Where(a => a.audit.Exception != null);
4244
foreach (var a in auditExceptions)
4345
sb.AppendLine($"# Audit exception in step {a.i + 1} {a.audit.Event.GetStringValue()}:\r\n{a.audit.Exception}");

src/Elasticsearch.Net/Transport/Pipeline/RequestPipeline.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public ElasticsearchClientException CreateClientException<TResponse>(
210210
: $"Status code {statusCode} from: {callDetails.HttpMethod} {callDetails.Uri.PathAndQuery}";
211211

212212

213-
var exceptionMessage = innerException?.Message ?? $"Request failed to execute";
213+
var exceptionMessage = innerException?.Message ?? "Request failed to execute";
214214

215215
var pipelineFailure = data.OnFailurePipelineFailure;
216216
if (pipelineExceptions.HasAny())

src/Nest/Document/Multiple/BulkAll/BulkAllObservable.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ private async Task<BulkAllResponse> BulkAsync(IList<T> buffer, long page, int ba
128128
.ConfigureAwait(false);
129129

130130
_compositeCancelToken.ThrowIfCancellationRequested();
131-
132131
_bulkResponseCallback?.Invoke(response);
133132

134133
if (!response.ApiCall.Success)
@@ -166,7 +165,7 @@ private void HandleDroppedDocuments(List<Tuple<BulkResponseItemBase, T>> dropped
166165

167166
foreach (var dropped in droppedDocuments) _droppedDocumentCallBack(dropped.Item1, dropped.Item2);
168167
if (!_partitionedBulkRequest.ContinueAfterDroppedDocuments)
169-
throw ThrowOnBadBulk(response, $"BulkAll halted after receiving failures that can not be retried from _bulk");
168+
throw ThrowOnBadBulk(response, $"{nameof(BulkAll)} halted after receiving failures that can not be retried from _bulk");
170169
}
171170

172171
private async Task<BulkAllResponse> HandleBulkRequest(IList<T> buffer, long page, int backOffRetries, BulkResponse response)
@@ -178,7 +177,7 @@ private async Task<BulkAllResponse> HandleBulkRequest(IList<T> buffer, long page
178177
{
179178
case PipelineFailure.MaxRetriesReached:
180179
if (response.ApiCall.AuditTrail.Last().Event == AuditEvent.FailedOverAllNodes)
181-
throw ThrowOnBadBulk(response, $"BulkAll halted after attempted bulk failed over all the active nodes");
180+
throw ThrowOnBadBulk(response, $"{nameof(BulkAll)} halted after attempted bulk failed over all the active nodes");
182181

183182
ThrowOnExhaustedRetries();
184183
return await RetryDocuments(page, ++backOffRetries, buffer).ConfigureAwait(false);
@@ -187,7 +186,7 @@ private async Task<BulkAllResponse> HandleBulkRequest(IList<T> buffer, long page
187186
case PipelineFailure.NoNodesAttempted:
188187
case PipelineFailure.SniffFailure:
189188
case PipelineFailure.Unexpected:
190-
throw ThrowOnBadBulk(response, $"BulkAll halted after {nameof(PipelineFailure)}.{reason} from _bulk");
189+
throw ThrowOnBadBulk(response, $"{nameof(BulkAll)} halted after {nameof(PipelineFailure)}.{reason} from _bulk");
191190
case PipelineFailure.BadResponse:
192191
case PipelineFailure.PingFailure:
193192
case PipelineFailure.MaxTimeoutReached:
@@ -202,7 +201,7 @@ void ThrowOnExhaustedRetries()
202201
if (_partitionedBulkRequest.ContinueAfterDroppedDocuments || backOffRetries < _backOffRetries) return;
203202

204203
throw ThrowOnBadBulk(response,
205-
$"BulkAll halted after {nameof(PipelineFailure)}.{reason} from _bulk and exhausting retries ({backOffRetries})"
204+
$"{nameof(BulkAll)} halted after {nameof(PipelineFailure)}.{reason} from _bulk and exhausting retries ({backOffRetries})"
206205
);
207206
}
208207
}

0 commit comments

Comments
 (0)