5

I am using the package graphql_flutter for GraphQL operations in my flutter app. The queries and mutations are going well but I cannot retrieve the errors by following the ways mentioned in their doc. Every time I receive a generic error message which is,

ClientException: Failed to connect to http://127.0.0.1:3006/graphql: 

That I get by doing,

print(result.exception.toString());

My mutation looks like,

final MutationOptions mutationOptions = MutationOptions(
  documentNode: gql(mutationString),
  variables: vars
);

final QueryResult result = await _instance._client.mutate(mutationOptions);

if (result.hasException) {
  // none of the following prints the expected error.
  print(result.exception.clientException.message);
  print(result.exception.graphqlErrors);
  print(result.exception.toString());
}

print(result.data);

return result.data;

Whereas in the apollo client, My error is :

{
  "errors": [
    {
      "message": "Invalid Phone number provided",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "otp"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
         ....

But I get none of that.

Note: The success response is coming as expected. I would like to know how can I get the graphql errors.

3 Answers 3

4

I found the problem. It is because android cannot connect to 127.0.0.1 or localhost from the emulator. I replaced the host with my local IP address and it is working fine now.

0

Put this in a try/ catch block and see if it can catch any exceptions

final QueryResult result = await _instance._client.mutate(mutationOptions);
5
  • after that line i am able to print result.exception so obviously there is no exception coming from the _client.mutate call. It is already handled inside of that function.
    – Anik
    Jun 22, 2020 at 8:34
  • Then the error should be inside the result data instead of result exception. The exception is only for syntax and network-related issues. Not the exception thrown by the middleware. Jun 22, 2020 at 9:17
  • You should check these errors manually, like result.data["errors"] != null or somethng Jun 22, 2020 at 9:18
  • result.data prints null, and " The exception is only for syntax and network-related issues" - DIdn't find that anywhere in the documentation, I did the way the documentation mentions.
    – Anik
    Jun 22, 2020 at 13:45
  • It should not be. GraphQL returns status code 200 for all the results. It's a thing of the GraphQL server. You will have to manually check if the result is actual data or errors. Jun 23, 2020 at 9:46
0

the original question was how to get the graphql errors. I'm using graphql: ^5.0.0

I checked the docs and found this example:

if (result.hasException) {
  if (result.exception.linkException is NetworkException) {
     // handle network issues, maybe
    }
   return Text(result.exception.toString())
 }

but that just gave me the exception, not the error, I casted the result exception to the type of error and was able to get the message:

if (result.hasException) {
    if (result.exception!.linkException is ServerException) {
      ServerException exception =
          result.exception!.linkException as ServerException;
      var errorMessage = exception.parsedResponse!.errors![0].message;
      print(errorMessage);
      throw Exception(errorMessage);
    }
  } 

this seems like a lot of work for a simple message, I wonder if there is another simpler built-in way to do it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.