From fe69be536b261f595b131eef6e619cacabeb87b1 Mon Sep 17 00:00:00 2001 From: Jericho Date: Fri, 15 Nov 2024 10:09:42 -0500 Subject: [PATCH] Improve error message to include field name when available. Resolves #378 --- .../Extensions/InternalTests.cs | 17 +++++++++++++++++ Source/ZoomNet/Extensions/Internal.cs | 7 ++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Source/ZoomNet.UnitTests/Extensions/InternalTests.cs b/Source/ZoomNet.UnitTests/Extensions/InternalTests.cs index a2f07779..a58e1ec9 100644 --- a/Source/ZoomNet.UnitTests/Extensions/InternalTests.cs +++ b/Source/ZoomNet.UnitTests/Extensions/InternalTests.cs @@ -417,6 +417,23 @@ public async Task CanHandleUnescapedDoubleQuotesInErrorMessage() errorMessage.ShouldStartWith("Invalid access token, does not contain scopes"); errorCode.ShouldBe(104); } + + [Fact] + public async Task IncludesFieldNameInErrorMessage() + { + // Arrange + const string responseContent = @"{""code"":300,""message"":""Validation Failed."",""errors"":[{""field"":""settings.jbh_time"",""message"":""Invalid parameter: jbh_time.""}]}"; + var message = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(responseContent) }; + var response = new MockFluentHttpResponse(message, null, CancellationToken.None); + + // Act + var (isError, errorMessage, errorCode) = await response.Message.GetErrorMessageAsync(); + + // Assert + isError.ShouldBeTrue(); + errorMessage.ShouldBe("Validation Failed. settings.jbh_time Invalid parameter: jbh_time."); + errorCode.ShouldBe(300); + } } } } diff --git a/Source/ZoomNet/Extensions/Internal.cs b/Source/ZoomNet/Extensions/Internal.cs index 2e749d8f..3a88b984 100644 --- a/Source/ZoomNet/Extensions/Internal.cs +++ b/Source/ZoomNet/Extensions/Internal.cs @@ -748,7 +748,12 @@ internal static DiagnosticInfo GetDiagnosticInfo(this IResponse response) " ", jsonErrorDetails .EnumerateArray() - .Select(jsonErrorDetail => jsonErrorDetail.TryGetProperty("message", out JsonElement jsonErrorMessage) ? jsonErrorMessage.GetString() : string.Empty) + .Select(jsonErrorDetail => + { + var field = jsonErrorDetail.TryGetProperty("field", out JsonElement jsonField) ? jsonField.GetString() : string.Empty; + var message = jsonErrorDetail.TryGetProperty("message", out JsonElement jsonErrorMessage) ? jsonErrorMessage.GetString() : string.Empty; + return $"{field} {message}".Trim(); + }) .Where(message => !string.IsNullOrEmpty(message))); if (!string.IsNullOrEmpty(errorDetails)) errorMessage += $" {errorDetails}";