Skip to content

Commit cc382c9

Browse files
authored
Properly handle async calls when logging request/response bodies (#973)
* Properly handle async calls. * Make regex static compiled. * Catch and write all exceptions to the error stream.
1 parent 05c7b54 commit cc382c9

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

src/readme.graph.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,11 @@ directive:
534534
let duplicateDebugRegex = /^(\s*)(WriteDebug\(\$"{id}:.*)/gmi
535535
$ = $.replace(duplicateDebugRegex, "");
536536
537+
// catch all exceptions in ProcessRecordAsync.
538+
let processAsyncFinallyRegex = /(finally\s*{\s*await \(\(Microsoft\.Graph\.PowerShell\.Runtime\.IEventListener\)this\)\.Signal\(Microsoft\.Graph\.PowerShell\.Runtime\.Events\.CmdletProcessRecordAsyncEnd\);)/gmi
539+
let catchAllExceptionImplementation = '((Runtime.IEventListener)this).Signal(Runtime.Events.CmdletException, $"{ex.GetType().Name} - {ex.Message} : {ex.StackTrace}").Wait(); if (((Runtime.IEventListener)this).Token.IsCancellationRequested) { return; } WriteError(new global::System.Management.Automation.ErrorRecord(ex, string.Empty, global::System.Management.Automation.ErrorCategory.NotSpecified, null));'
540+
$ = $.replace(processAsyncFinallyRegex, `catch (System.Exception ex){${catchAllExceptionImplementation}}\n$1`);
541+
537542
return $;
538543
}
539544

tools/Custom/HttpMessageLogFormatter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static string GetHttpRequestLog(HttpRequestMessage request)
2424
string body = string.Empty;
2525
try
2626
{
27-
body = (request.Content == null) ? string.Empty : FormatString(request.Content.ReadAsStringAsync().Result);
27+
body = (request.Content == null) ? string.Empty : FormatString(request.Content.ReadAsStringAsync().GetAwaiter().GetResult());
2828
}
2929
catch { }
3030

@@ -44,7 +44,7 @@ public static string GetHttpResponseLog(HttpResponseMessage response)
4444
string body = string.Empty;
4545
try
4646
{
47-
body = (response.Content == null) ? string.Empty : FormatString(response.Content.ReadAsStringAsync().Result);
47+
body = (response.Content == null) ? string.Empty : FormatString(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
4848
}
4949
catch { }
5050

@@ -56,11 +56,11 @@ public static string GetHttpResponseLog(HttpResponseMessage response)
5656
return stringBuilder.ToString();
5757
}
5858

59+
private static Regex regexPattern = new Regex("(\\s*\"access_token\"\\s*:\\s*)\"[^\"]+\"", RegexOptions.Compiled);
5960
private static object SanitizeBody(string body)
6061
{
6162
IList<Regex> regexList = new List<Regex>();
6263
// Remove access_token:* instances in body.
63-
Regex regexPattern = new Regex("(\\s*\"access_token\"\\s*:\\s*)\"[^\"]+\"");
6464
regexList.Add(regexPattern);
6565

6666
foreach (Regex matcher in regexList)

tools/Custom/Module.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private async Task OnResponseCreated(string id, CancellationToken cancellationTo
105105
var response = eventData?.ResponseMessage as HttpResponseMessage;
106106
if (response != null)
107107
{
108-
if (response.Headers.Warning != null)
108+
if (response.Headers.Warning != null && response.Headers.Warning.Any())
109109
{
110110
string warningHeader = response.Headers.Warning.ToString();
111111
await signal(Events.Warning, cancellationToken,

0 commit comments

Comments
 (0)