Skip to content

Commit d2d0e2e

Browse files
committed
Log response content.
1 parent af7a8cb commit d2d0e2e

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

src/readme.graph.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ directive:
422422
423423
// Format error details.
424424
let errorDetailsRegex = /(ErrorDetails\s*=\s*)(new.*ErrorDetails\(message\).*)/gmi
425-
$ = $.replace(errorDetailsRegex, '$1this.GetErrorDetails((await response)?.Error, responseMessage)');
425+
$ = $.replace(errorDetailsRegex, '$1await this.GetErrorDetailsAsync((await response)?.Error, responseMessage)');
426426
427427
return $;
428428
}

tools/Custom/HttpMessageLogFormatter.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,24 @@ public static async Task<string> GetHttpResponseLogAsync(HttpResponseMessage res
8989
return stringBuilder.ToString();
9090
}
9191

92+
public static async Task<string> GetErrorLogAsync(HttpResponseMessage response)
93+
{
94+
if (response == null) return string.Empty;
95+
96+
string body = string.Empty;
97+
try
98+
{
99+
body = (response.Content == null) ? string.Empty : FormatString(await response.Content.ReadAsStringAsync());
100+
}
101+
catch { }
102+
103+
StringBuilder stringBuilder = new StringBuilder();
104+
stringBuilder.AppendLine($"Status:{((int)response.StatusCode)} ({response.StatusCode}){Environment.NewLine}");
105+
stringBuilder.AppendLine($"Content:{Environment.NewLine}{body}{Environment.NewLine}");
106+
stringBuilder.AppendLine($"Headers:{Environment.NewLine}{HeadersToString(response.Headers)}{Environment.NewLine}");
107+
return stringBuilder.ToString();
108+
}
109+
92110
internal static string HeadersToString(HttpHeaders headers)
93111
{
94112
return HeadersToString(ConvertHttpHeadersToCollection(headers));

tools/Custom/PSCmdletExtensions.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,11 @@ internal static void WriteToFile(this PSCmdlet cmdlet, HttpResponseMessage respo
106106
}
107107
}
108108

109-
internal static ErrorDetails GetErrorDetails(this PSCmdlet cmdlet, IMicrosoftGraphODataErrorsMainError odataError, HttpResponseMessage responseMessage)
109+
internal static async Task<ErrorDetails> GetErrorDetailsAsync(this PSCmdlet cmdlet, IMicrosoftGraphODataErrorsMainError odataError, HttpResponseMessage response)
110110
{
111111
var serviceErrorDoc = "https://learn.microsoft.com/graph/errors";
112112
var recommendedAction = $"See service error codes: {serviceErrorDoc}";
113-
var requestUri = $"{responseMessage.RequestMessage?.Method} {responseMessage.RequestMessage?.RequestUri}";
114-
string date = odataError?.Innererror?.Date;
115-
string requestId = odataError?.Innererror?.RequestId;
116-
string clientRequestId = odataError?.Innererror?.ClientRequestId;
117-
string errorDetailsMessage = $"{odataError?.Message}\n\n{requestUri}\nStatusCode: {responseMessage.StatusCode}\nErrorCode: {odataError?.Code}\nDate: {date}\nRequestId: {requestId}\nClientRequestId: {clientRequestId}\n\n{recommendedAction}";
113+
var errorDetailsMessage = await HttpMessageLogFormatter.GetErrorLogAsync(response);
118114
return new ErrorDetails(errorDetailsMessage)
119115
{
120116
RecommendedAction = recommendedAction

0 commit comments

Comments
 (0)