Skip to content

Commit

Permalink
Improve error message to include field name when available.
Browse files Browse the repository at this point in the history
Resolves #378
  • Loading branch information
Jericho committed Nov 15, 2024
1 parent daf63a4 commit fe69be5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
17 changes: 17 additions & 0 deletions Source/ZoomNet.UnitTests/Extensions/InternalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
7 changes: 6 additions & 1 deletion Source/ZoomNet/Extensions/Internal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
Expand Down

0 comments on commit fe69be5

Please sign in to comment.