Skip to content

Commit 3cffc9f

Browse files
committed
Log Microsoft Graph error codes.
1 parent 296ba2d commit 3cffc9f

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

tools/Custom/HttpMessageLogFormatter.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace NamespacePrefixPlaceholder.PowerShell
66
{
7+
using NamespacePrefixPlaceholder.PowerShell.Models;
78
using Newtonsoft.Json;
89
using System;
910
using System.Collections.Generic;
@@ -89,20 +90,15 @@ public static async Task<string> GetHttpResponseLogAsync(HttpResponseMessage res
8990
return stringBuilder.ToString();
9091
}
9192

92-
public static async Task<string> GetErrorLogAsync(HttpResponseMessage response)
93+
public static async Task<string> GetErrorLogAsync(HttpResponseMessage response, IMicrosoftGraphODataErrorsMainError odataError)
9394
{
9495
if (response == null) return string.Empty;
9596

96-
string body = string.Empty;
97-
try
98-
{
99-
body = (response.Content == null) ? string.Empty : await response.Content.ReadAsStringAsync();
100-
}
101-
catch { }
102-
10397
StringBuilder stringBuilder = new StringBuilder();
104-
stringBuilder.AppendLine($"Status:{((int)response.StatusCode)} ({response.StatusCode}){Environment.NewLine}");
105-
stringBuilder.AppendLine($"Content:{Environment.NewLine}{body}{Environment.NewLine}");
98+
stringBuilder.AppendLine($"{odataError?.Message}{Environment.NewLine}");
99+
stringBuilder.AppendLine($"Status: {((int)response.StatusCode)} ({response.StatusCode})");
100+
stringBuilder.AppendLine($"ErrorCode: {odataError?.Code}");
101+
stringBuilder.AppendLine($"Date: {odataError?.Innererror?.Date}{Environment.NewLine}");
106102
stringBuilder.AppendLine($"Headers:{Environment.NewLine}{HeadersToString(response.Headers)}{Environment.NewLine}");
107103
return stringBuilder.ToString();
108104
}

tools/Custom/PSCmdletExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ internal static void WriteToFile(this PSCmdlet cmdlet, HttpResponseMessage respo
105105
cmdlet.WriteToStream(inputStream, fileProvider.Stream, downloadUrl, cancellationToken);
106106
}
107107
}
108-
108+
109109
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 errorDetailsMessage = await HttpMessageLogFormatter.GetErrorLogAsync(response);
114-
return new ErrorDetails($"{odataError?.Message}{Environment.NewLine}{errorDetailsMessage}")
113+
var errorDetailsMessage = await HttpMessageLogFormatter.GetErrorLogAsync(response, odataError);
114+
return new ErrorDetails(errorDetailsMessage)
115115
{
116116
RecommendedAction = recommendedAction
117117
};

0 commit comments

Comments
 (0)