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 Sep 25, 2022
1 parent e6f88a5 commit 3b1dfe4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
24 changes: 20 additions & 4 deletions Source/ZoomNet/Extensions/Internal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,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 @@ -696,8 +704,14 @@ internal static (WeakReference<HttpRequestMessage> RequestReference, string Diag
try
{
var rootJsonElement = JsonDocument.Parse(responseContent).RootElement;
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(
Expand All @@ -713,13 +727,15 @@ internal static (WeakReference<HttpRequestMessage> RequestReference, string Diag

if (!string.IsNullOrEmpty(errorDetails)) errorMessage += $" {errorDetails}";
}

isError = errorCode.HasValue;
}
catch
{
// Intentionally ignore parsing errors
}
finally
{
isError = errorCode.HasValue;
}
}

return (isError, errorMessage, errorCode);
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 3b1dfe4

Please sign in to comment.