|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace Elasticsearch.Net |
| 7 | +{ |
| 8 | + public static class ResponseStatics |
| 9 | + { |
| 10 | + private static readonly string RequestAlreadyCaptured = |
| 11 | + "<Request stream not captured or already read to completion by serializer. Set DisableDirectStreaming() on ConnectionSettings to force it to be set on the response.>"; |
| 12 | + |
| 13 | + private static readonly string ResponseAlreadyCaptured = |
| 14 | + "<Response stream not captured or already read to completion by serializer. Set DisableDirectStreaming() on ConnectionSettings to force it to be set on the response.>"; |
| 15 | + |
| 16 | + public static string DebugInformationBuilder(IApiCallDetails r, StringBuilder sb) |
| 17 | + { |
| 18 | + if (r.DeprecationWarnings.HasAny()) |
| 19 | + { |
| 20 | + sb.AppendLine($"# Server indicated deprecations:"); |
| 21 | + foreach (var deprecation in r.DeprecationWarnings) |
| 22 | + sb.AppendLine($"- {deprecation}"); |
| 23 | + } |
| 24 | + sb.AppendLine($"# Audit trail of this API call:"); |
| 25 | + var auditTrail = (r.AuditTrail ?? Enumerable.Empty<Audit>()).ToList(); |
| 26 | + DebugAuditTrail(auditTrail, sb); |
| 27 | + if (r.ServerError != null) sb.AppendLine($"# ServerError: {r.ServerError}"); |
| 28 | + if (r.OriginalException != null) sb.AppendLine($"# OriginalException: {r.OriginalException}"); |
| 29 | + DebugAuditTrailExceptions(auditTrail, sb); |
| 30 | + |
| 31 | + var response = r.ResponseBodyInBytes?.Utf8String() ?? ResponseAlreadyCaptured; |
| 32 | + var request = r.RequestBodyInBytes?.Utf8String() ?? RequestAlreadyCaptured; |
| 33 | + sb.AppendLine($"# Request:\r\n{request}"); |
| 34 | + sb.AppendLine($"# Response:\r\n{response}"); |
| 35 | + |
| 36 | + return sb.ToString(); |
| 37 | + } |
| 38 | + |
| 39 | + public static void DebugAuditTrailExceptions(List<Audit> auditTrail, StringBuilder sb) |
| 40 | + { |
| 41 | + var auditExceptions = auditTrail.Select((audit, i) => new { audit, i }).Where(a => a.audit.Exception != null); |
| 42 | + foreach (var a in auditExceptions) |
| 43 | + sb.AppendLine($"# Audit exception in step {a.i + 1} {a.audit.Event.GetStringValue()}:\r\n{a.audit.Exception}"); |
| 44 | + } |
| 45 | + |
| 46 | + public static void DebugAuditTrail(List<Audit> auditTrail, StringBuilder sb) |
| 47 | + { |
| 48 | + if (auditTrail == null) return; |
| 49 | + |
| 50 | + foreach (var a in auditTrail.Select((a, i) => new { a, i })) |
| 51 | + { |
| 52 | + var audit = a.a; |
| 53 | + sb.Append($" - [{a.i + 1}] {audit.Event.GetStringValue()}:"); |
| 54 | + |
| 55 | + AuditNodeUrl(sb, audit); |
| 56 | + |
| 57 | + if (audit.Exception != null) sb.Append($" Exception: {audit.Exception.GetType().Name}"); |
| 58 | + sb.AppendLine($" Took: {(audit.Ended - audit.Started).ToString()}"); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private static void AuditNodeUrl(StringBuilder sb, Audit audit) |
| 63 | + { |
| 64 | + var uri = audit.Node?.Uri; |
| 65 | + if (uri == null) return; |
| 66 | + |
| 67 | + if (!string.IsNullOrEmpty(uri.UserInfo)) |
| 68 | + { |
| 69 | + var builder = new UriBuilder(uri); |
| 70 | + builder.Password = "redacted"; |
| 71 | + uri = builder.Uri; |
| 72 | + } |
| 73 | + sb.Append($" Node: {uri}"); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments