NavidK0 / SimpleGraphQL-For-Unity

A simple graphQL client that allows one to use .graphql files (or code) for queries, mutations, and subscriptions with Unity.
MIT License
34 stars 18 forks source link

API hides cause of http errors #18

Closed johanhelsing closed 3 years ago

johanhelsing commented 3 years ago

It's currently hard to do proper error handling with this library.

Normal GraphQL error responses from the server are more or less fine, the response object will have an Errors field, which can be checked.

However, if something else fails when calling GraphQLClient.Send that is not a GraphQL error response, the method will simply return null or an empty string.

This is because HttpUtils.PostRequestAsync returns webRequest.downloadHandler.text without checking for or passing on webRequest.error or webRequest.result.

This means the following logic can't currently be implemented:

if (connection error)
  wait and try again
if (http 401 unauthorized)
  log in and try again
if (http 404 etc)
  give up

One quite simple solution might be to throw exceptions that wrap the UnityWebRequest.

Personally, I would prefer not throwing and instead return something like rust's Result types, so it's obvious that something can fail and that you'd have to handle errors. However it looks like we'd have to wait for C# 9 enum class support in Unity to implement this in a clean way.

@NavidK0 Perhaps it's better to go with a simple stupid approach and add a UnityWebRequestException class to the API? Then we could change it later?