Skip to content

Commit

Permalink
(GH-220) Improve how we check for errors in Zoom API responses to acc…
Browse files Browse the repository at this point in the history
…ount for the fact that error JSON from Contact Center is slightly different
  • Loading branch information
Jericho committed Oct 16, 2022
1 parent 29431f3 commit ea17d1c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
23 changes: 16 additions & 7 deletions Source/ZoomNet/Extensions/Internal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,14 @@ internal static (WeakReference<HttpRequestMessage> RequestReference, string Diag
}
]
}
There are some cases where error information is returned in a JSON string structured slightly differently.
For instance, ContactCenter endpoints return error information like this:
{
"status":false,
"errorCode":"401", <-- Notice this node contains a string
"errorMessage":"Contact Center has not been enabled for this account."
}
*/

var responseContent = await message.Content.ReadAsStringAsync(null).ConfigureAwait(false);
Expand All @@ -687,19 +695,20 @@ internal static (WeakReference<HttpRequestMessage> RequestReference, string Diag

if (rootJsonElement.ValueKind == JsonValueKind.Object)
{
errorCode = rootJsonElement.TryGetProperty("code", out JsonElement jsonErrorCode) ? (int?)jsonErrorCode.GetInt32() : (int?)null;
errorMessage = rootJsonElement.TryGetProperty("message", out JsonElement jsonErrorMessage) ? jsonErrorMessage.GetString() : (errorCode.HasValue ? $"Error code: {errorCode}" : errorMessage);
if (rootJsonElement.TryGetProperty("code", out JsonElement codeJsonProperty)) errorCode = codeJsonProperty.GetInt32();
else if (rootJsonElement.TryGetProperty("errorCode", out JsonElement errorCodeJsonProperty)) errorCode = int.Parse(errorCodeJsonProperty.GetString());

if (rootJsonElement.TryGetProperty("message", out JsonElement messageJsonProperty)) errorMessage = messageJsonProperty.GetString();
else if (rootJsonElement.TryGetProperty("errorMessage", out JsonElement errorMessageJsonProperty)) errorMessage = errorMessageJsonProperty.GetString();
else if (errorCode.HasValue) errorMessage = $"Error code: {errorCode}";

if (rootJsonElement.TryGetProperty("errors", out JsonElement jsonErrorDetails))
{
var errorDetails = string.Join(
" ",
jsonErrorDetails
.EnumerateArray()
.Select(jsonErrorDetail =>
{
var errorDetail = jsonErrorDetail.TryGetProperty("message", out JsonElement jsonErrorMessage) ? jsonErrorMessage.GetString() : string.Empty;
return errorDetail;
})
.Select(jsonErrorDetail => jsonErrorDetail.TryGetProperty("message", out JsonElement jsonErrorMessage) ? jsonErrorMessage.GetString() : string.Empty)
.Where(message => !string.IsNullOrEmpty(message)));

if (!string.IsNullOrEmpty(errorDetails)) errorMessage += $" {errorDetails}";
Expand Down
8 changes: 3 additions & 5 deletions Source/ZoomNet/Utilities/ZoomRetryCoordinator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -63,10 +62,9 @@ public async Task<HttpResponseMessage> ExecuteAsync(IRequest request, Func<IRequ
// Check if the token needs to be refreshed
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
var responseContent = await response.Content.ReadAsStringAsync(null).ConfigureAwait(false);
var jsonResponse = JsonDocument.Parse(responseContent).RootElement;
var message = jsonResponse.GetPropertyValue("message", string.Empty);
if (message.StartsWith("access token is expired", StringComparison.OrdinalIgnoreCase))
var (isError, errorMessage, errorCode) = await response.GetErrorMessageAsync().ConfigureAwait(false);

if (errorMessage.StartsWith("access token is expired", StringComparison.OrdinalIgnoreCase))
{
var refreshedToken = RefreshToken();
response = await _defaultRetryCoordinator.ExecuteAsync(request.WithBearerAuthentication(refreshedToken), dispatcher);
Expand Down

0 comments on commit ea17d1c

Please sign in to comment.