From d2150728011b7b8989bdb79f233ac30fa910975a Mon Sep 17 00:00:00 2001 From: Peter Ombwa Date: Fri, 10 Dec 2021 12:40:56 -0800 Subject: [PATCH 1/3] Properly handle async calls. --- tools/Custom/HttpMessageLogFormatter.cs | 4 ++-- tools/Custom/Module.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/Custom/HttpMessageLogFormatter.cs b/tools/Custom/HttpMessageLogFormatter.cs index f743b9b685b..4305aa3cfbd 100644 --- a/tools/Custom/HttpMessageLogFormatter.cs +++ b/tools/Custom/HttpMessageLogFormatter.cs @@ -24,7 +24,7 @@ public static string GetHttpRequestLog(HttpRequestMessage request) string body = string.Empty; try { - body = (request.Content == null) ? string.Empty : FormatString(request.Content.ReadAsStringAsync().Result); + body = (request.Content == null) ? string.Empty : FormatString(request.Content.ReadAsStringAsync().GetAwaiter().GetResult()); } catch { } @@ -44,7 +44,7 @@ public static string GetHttpResponseLog(HttpResponseMessage response) string body = string.Empty; try { - body = (response.Content == null) ? string.Empty : FormatString(response.Content.ReadAsStringAsync().Result); + body = (response.Content == null) ? string.Empty : FormatString(response.Content.ReadAsStringAsync().GetAwaiter().GetResult()); } catch { } diff --git a/tools/Custom/Module.cs b/tools/Custom/Module.cs index 4c0422f24b7..bb466811e53 100644 --- a/tools/Custom/Module.cs +++ b/tools/Custom/Module.cs @@ -105,7 +105,7 @@ private async Task OnResponseCreated(string id, CancellationToken cancellationTo var response = eventData?.ResponseMessage as HttpResponseMessage; if (response != null) { - if (response.Headers.Warning != null) + if (response.Headers.Warning != null && response.Headers.Warning.Any()) { string warningHeader = response.Headers.Warning.ToString(); await signal(Events.Warning, cancellationToken, From 506cd245a0b3e406991d97fbfc0c725da6441fff Mon Sep 17 00:00:00 2001 From: Peter Ombwa Date: Fri, 10 Dec 2021 12:48:40 -0800 Subject: [PATCH 2/3] Make regex static compiled. --- tools/Custom/HttpMessageLogFormatter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/Custom/HttpMessageLogFormatter.cs b/tools/Custom/HttpMessageLogFormatter.cs index 4305aa3cfbd..6c6cce78b48 100644 --- a/tools/Custom/HttpMessageLogFormatter.cs +++ b/tools/Custom/HttpMessageLogFormatter.cs @@ -56,11 +56,11 @@ public static string GetHttpResponseLog(HttpResponseMessage response) return stringBuilder.ToString(); } + private static Regex regexPattern = new Regex("(\\s*\"access_token\"\\s*:\\s*)\"[^\"]+\"", RegexOptions.Compiled); private static object SanitizeBody(string body) { IList regexList = new List(); // Remove access_token:* instances in body. - Regex regexPattern = new Regex("(\\s*\"access_token\"\\s*:\\s*)\"[^\"]+\""); regexList.Add(regexPattern); foreach (Regex matcher in regexList) From db43c261aff9d1e78805cc7c021537902d60c120 Mon Sep 17 00:00:00 2001 From: Peter Ombwa Date: Tue, 14 Dec 2021 12:37:28 -0800 Subject: [PATCH 3/3] Catch and write all exceptions to the error stream. --- src/readme.graph.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/readme.graph.md b/src/readme.graph.md index 11ec21df545..3845cdc9070 100644 --- a/src/readme.graph.md +++ b/src/readme.graph.md @@ -534,6 +534,11 @@ directive: let duplicateDebugRegex = /^(\s*)(WriteDebug\(\$"{id}:.*)/gmi $ = $.replace(duplicateDebugRegex, ""); + // catch all exceptions in ProcessRecordAsync. + let processAsyncFinallyRegex = /(finally\s*{\s*await \(\(Microsoft\.Graph\.PowerShell\.Runtime\.IEventListener\)this\)\.Signal\(Microsoft\.Graph\.PowerShell\.Runtime\.Events\.CmdletProcessRecordAsyncEnd\);)/gmi + 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));' + $ = $.replace(processAsyncFinallyRegex, `catch (System.Exception ex){${catchAllExceptionImplementation}}\n$1`); + return $; }