Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CG.Web.MegaApiClient.MegaApiClient.Login > Unable to cast object of type 'System.Int64' to type 'Newtonsoft.Json.Linq.JArray' #235

Closed
dhabierre opened this issue Dec 18, 2023 · 3 comments · Fixed by #236
Milestone

Comments

@dhabierre
Copy link
Contributor

dhabierre commented Dec 18, 2023

Description:

The error occurres while executing the MegaApiClient LoginAsync method (~1 year without any error):

private readonly MegaApiClient client = new();

[...]

private async Task<MegaApiClient> GetClientAsync()
{
    if (!this.client.IsLoggedIn)
    {
        var options = this.options.Mega;

        await this.client.LoginAsync(options.Username, options.Password);
    }

    return this.client;
}

Locally on my dev computer, no issue, everything is OK.

But once the app is deployed on my VM provider:

System.InvalidCastException: Unable to cast object of type 'System.Int64' to type 'Newtonsoft.Json.Linq.JArray'
   at CG.Web.MegaApiClient.MegaApiClient.RequestCore[TResponse](RequestBase request, Byte[] key)\n  
   at CG.Web.MegaApiClient.MegaApiClient.Request[TResponse](RequestBase request, Byte[] key)\n  
   at CG.Web.MegaApiClient.MegaApiClient.GenerateAuthInfos(String email, String password, String mfaKey)\n   
   at CG.Web.MegaApiClient.MegaApiClient.Login(String email, String password, String mfaKey)\n   
   at CG.Web.MegaApiClient.MegaApiClient.<>c__DisplayClass62_0.<LoginAsync>b__0()\n   
   at System.Threading.Tasks.Task`1.InnerInvoke()\n   
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)\n--- End of stack trace from previous location ---\n   
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)\n   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)\n--- End of stack trace from previous location ---\n   
   at ****.Infrastructure.CloudStorage.MegaCloudStorageService.GetClientAsync() in D:\\a\\1\\s\\src\\Backend\\3-Infrastructure\\****.Infrastructure.CloudStorage\\MegaCloudStorageService.cs:line 152\n   
   ...

MegaApiClient Version: 1.10.3

Thanks.

@dhabierre
Copy link
Contributor Author

dhabierre commented Dec 20, 2023

Something like this fix the return (report to comments):

private TResponse RequestCore<TResponse>(RequestBase request, byte[] key)
    where TResponse : class
{
  var dataRequest = JsonConvert.SerializeObject(new object[] { request });
  var uri = GenerateUrl(request.QueryArguments);
  object jsonData = null;
  var attempt = 0;
  // Added line
  var apiCode = ApiResultCode.Ok;
  while (_options.ComputeApiRequestRetryWaitDelay(++attempt, out var retryDelay))
  {
    var dataResult = _webClient.PostRequestJson(uri, dataRequest);

    if (string.IsNullOrEmpty(dataResult)
      || (jsonData = JsonConvert.DeserializeObject(dataResult)) == null
      || jsonData is long
      || jsonData is JArray array && array[0].Type == JTokenType.Integer)
    {
      // Remove var keyword
      apiCode = jsonData == null
        ? ApiResultCode.RequestFailedRetry
        : jsonData is long
          ? (ApiResultCode)Enum.ToObject(typeof(ApiResultCode), jsonData)
          : (ApiResultCode)((JArray)jsonData)[0].Value<int>();

      if (apiCode != ApiResultCode.Ok)
      {
        ApiRequestFailed?.Invoke(this, new ApiRequestFailedEventArgs(uri, attempt, retryDelay, apiCode, dataResult));
      }

      if (apiCode == ApiResultCode.RequestFailedRetry)
      {
        Wait(retryDelay);
        continue;
      }

      if (apiCode != ApiResultCode.Ok)
      {
        throw new ApiException(apiCode);
      }
    }

    break;
  }

  // Added block
  if (apiCode != ApiResultCode.Ok)
  {
    throw new ApiException(apiCode);
  }

  var data = ((JArray)jsonData)[0].ToString();
  return (typeof(TResponse) == typeof(string)) ? data as TResponse : JsonConvert.DeserializeObject<TResponse>(data, new GetNodesResponseConverter(key));
}

@gpailler
Copy link
Owner

gpailler commented Dec 24, 2023

@dhabierre Thanks for the issue report. Would you mind proposing a PR?
Thanks !

@dhabierre
Copy link
Contributor Author

Hi @gpailler, I can do it at the end of the next week (in holidays at present time).

dhabierre added a commit to dhabierre/MegaApiClient that referenced this issue Dec 29, 2023
…pe 'Newtonsoft.Json.Linq.JArray' error

Github issue: gpailler#235
gpailler pushed a commit that referenced this issue Jan 21, 2024
…pe 'Newtonsoft.Json.Linq.JArray' error (#236)

Github issue: #235
@gpailler gpailler added this to the 1.10.4 milestone Jan 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants