Skip to content

LoadAsync crashing if {} is returned... openlibraryutility.cs #18

Description

@bradyguyc

In openlibraryutility.cs LoadAsync was throwing object null reference on the JToken.Parse(response) line if response was "{}" , note this occurs in my .NET 9 build of OpenLibrary.NET. I added the highlighted line to return null if {} is returned and all seems to work well now. I had the call to GetDataAsync surround with a try catch and I am not sure why the try catch didn't catch the exception.

 public async static Task<T?> LoadAsync<T>(HttpClient client, Uri uri, string path = "")
 {
     string response = await RequestAsync(client, uri);
     Debug.WriteLine("LoadAsync:"+response);
     if (string.IsNullOrWhiteSpace(path))
         return JsonConvert.DeserializeObject<T>(response);
     else
     {
         **_if (response == "{}") return default;_**
         JToken token = JToken.Parse(response);
         return token[path]!.ToObject<T>();
     }
 }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions