zino-hofmann / graphql-flutter

A GraphQL client for Flutter, bringing all the features from a modern GraphQL client to one easy to use package.
https://zino-hofmann.github.io/graphql-flutter
MIT License
3.25k stars 620 forks source link

Exceptions not being caught in try catch #1201

Closed joao-albach closed 2 years ago

joao-albach commented 2 years ago

Describe the issue When using the client whenever there is an erro in the request the exception is not caught, it does not matter if its in a try catch or i'm using onError it always crashes.

To Reproduce (MUST BE PROVIDED)

  GraphQLClient getClient(String baseUrl) {

    final HttpLink link = HttpLink(
      baseUrl,
    );

    GraphQLClient client = GraphQLClient(
      link: link,
      // The default store is the InMemoryStore, which does NOT persist to disk
      cache: GraphQLCache(),
    );

    return client;
  }

  Future<Thing> getThing() {
  try {
      final client = GqlClient().getClient(baseUrl);

      final result =
          await client.query(QueryOptions(document: gql(getThingQuery)));
} catch (e) {
      print(e.toString());
    }
  }

Expected behavior I expect the error to be printed instead of the app crashing, instead it crashed with this exceptions:

OperationException(linkException: ServerException(originalException: null, parsedResponse: Response(data: null, errors: [GraphQLError ......

device / execution context iOS simulator

vincenzopalazzo commented 2 years ago

Can you report a new issue also with the crash of onError??

I think with the actual API call you should check if there is an exception is hasException or something like that. and you can get the exception with result.exception

But your is a good point, maybe we could add a new method or introduce a way to support both supports both API

joao-albach commented 2 years ago

it is crashing before the i check for the result.hasException, but i just realized that it's an vscode debbug option... sorry

image