Skip to content

Commit 057cb71

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 (cherry picked from commit 4787e27)
1 parent add3f19 commit 057cb71

File tree

4 files changed

+40
-17
lines changed

4 files changed

+40
-17
lines changed

src/Elasticsearch.Net/Exceptions/ElasticsearchClientException.cs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
5+
using static Elasticsearch.Net.ResponseStatics;
56

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

37-
sb.AppendLine($"# FailureReason: {failureReason} while attempting {Request.Method.GetStringValue()} on {path}");
35+
sb.Append("# FailureReason: ")
36+
.Append(failureReason)
37+
.Append(" while attempting ");
38+
39+
if (Request != null)
40+
{
41+
sb.Append(Request.Method.GetStringValue()).Append(" on ");
42+
if (Request.Uri != null)
43+
sb.AppendLine(Request.Uri.ToString());
44+
else
45+
sb.Append(Request.PathAndQuery)
46+
.AppendLine(" on an empty node, likely a node predicate on ConnectionSettings not matching ANY nodes");
47+
}
48+
else if (Response != null)
49+
{
50+
sb.Append(Response.HttpMethod.GetStringValue())
51+
.Append(" on ")
52+
.AppendLine(Response.Uri.ToString());
53+
}
54+
else
55+
sb.AppendLine("a request");
56+
3857
if (Response != null)
39-
ResponseStatics.DebugInformationBuilder(Response, sb);
58+
DebugInformationBuilder(Response, sb);
4059
else
4160
{
42-
ResponseStatics.DebugAuditTrail(AuditTrail, sb);
43-
ResponseStatics.DebugAuditTrailExceptions(AuditTrail, sb);
61+
DebugAuditTrail(AuditTrail, sb);
62+
DebugAuditTrailExceptions(AuditTrail, sb);
4463
}
64+
4565
if (InnerException != null)
4666
{
47-
sb.AppendLine($"# Inner Exception: {InnerException.Message}");
48-
sb.AppendLine(InnerException.ToString());
67+
sb.Append("# Inner Exception: ")
68+
.AppendLine(InnerException.Message)
69+
.AppendLine(InnerException.ToString());
4970
}
50-
sb.AppendLine($"# Exception:");
51-
sb.AppendLine(ToString());
5271

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

src/Elasticsearch.Net/Responses/ResponseStatics.cs

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

3838
public static void DebugAuditTrailExceptions(List<Audit> auditTrail, StringBuilder sb)
3939
{
40+
if (auditTrail == null) return;
41+
4042
var auditExceptions = auditTrail.Select((audit, i) => new { audit, i }).Where(a => a.audit.Exception != null);
4143
foreach (var a in auditExceptions)
4244
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
@@ -212,7 +212,7 @@ public ElasticsearchClientException CreateClientException<TResponse>(
212212
: $"Status code {statusCode} from: {callDetails.HttpMethod} {callDetails.Uri.PathAndQuery}";
213213

214214

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

217217
var pipelineFailure = data.OnFailurePipelineFailure;
218218
if (pipelineExceptions.HasAny())

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,20 +176,20 @@ private void HandleDroppedDocuments(List<Tuple<IBulkResponseItem, T>> droppedDoc
176176

177177
foreach (var dropped in droppedDocuments) _droppedDocumentCallBack(dropped.Item1, dropped.Item2);
178178
if (!_partitionedBulkRequest.ContinueAfterDroppedDocuments)
179-
throw ThrowOnBadBulk(response, $"BulkAll halted after receiving failures that can not be retried from _bulk");
179+
throw ThrowOnBadBulk(response, $"{nameof(BulkAll)} halted after receiving failures that can not be retried from _bulk");
180180
}
181181

182182
private async Task<IBulkAllResponse> HandleBulkRequest(IList<T> buffer, long page, int backOffRetries, IBulkResponse response)
183183
{
184184
var clientException = response.ApiCall.OriginalException as ElasticsearchClientException;
185-
var failureReason = clientException?.FailureReason;
185+
var failureReason = clientException?.FailureReason;
186186
var reason = failureReason?.GetStringValue() ?? nameof(PipelineFailure.BadRequest);
187187
switch (failureReason)
188188
{
189189
case PipelineFailure.MaxRetriesReached:
190190
//TODO move this to its own PipelineFailure classification in 7.0
191191
if (response.ApiCall.AuditTrail.Last().Event == AuditEvent.FailedOverAllNodes)
192-
throw ThrowOnBadBulk(response, $"BulkAll halted after attempted bulk failed over all the active nodes");
192+
throw ThrowOnBadBulk(response, $"{nameof(BulkAll)} halted after attempted bulk failed over all the active nodes");
193193

194194
ThrowOnExhaustedRetries();
195195
return await RetryDocuments(page, ++backOffRetries, buffer).ConfigureAwait(false);
@@ -198,7 +198,7 @@ private async Task<IBulkAllResponse> HandleBulkRequest(IList<T> buffer, long pag
198198
case PipelineFailure.NoNodesAttempted:
199199
case PipelineFailure.SniffFailure:
200200
case PipelineFailure.Unexpected:
201-
throw ThrowOnBadBulk(response, $"BulkAll halted after {nameof(PipelineFailure)}.{reason} from _bulk");
201+
throw ThrowOnBadBulk(response, $"{nameof(BulkAll)} halted after {nameof(PipelineFailure)}.{reason} from _bulk");
202202
default:
203203
ThrowOnExhaustedRetries();
204204
return await RetryDocuments(page, ++backOffRetries, buffer).ConfigureAwait(false);
@@ -209,7 +209,7 @@ void ThrowOnExhaustedRetries()
209209
if (_partitionedBulkRequest.ContinueAfterDroppedDocuments || backOffRetries < _backOffRetries) return;
210210

211211
throw ThrowOnBadBulk(response,
212-
$"BulkAll halted after {nameof(PipelineFailure)}.{reason} from _bulk and exhausting retries ({backOffRetries})"
212+
$"{nameof(BulkAll)} halted after {nameof(PipelineFailure)}.{reason} from _bulk and exhausting retries ({backOffRetries})"
213213
);
214214
}
215215
}

0 commit comments

Comments
 (0)