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 Jun 11, 2022
1 parent d188fe1 commit 74f5fc1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
21 changes: 18 additions & 3 deletions Source/ZoomNet/Extensions/Internal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,14 @@ internal static (WeakReference<HttpRequestMessage> RequestReference, string Diag
"code": 300,
"message": "This meeting has not registration required: 544993922"
}
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 @@ -637,15 +645,22 @@ 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);

isError = errorCode.HasValue;
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}";
}
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 74f5fc1

Please sign in to comment.