How to handle errors via Ferry GraphQL

Viewed 207

community! Please give a clue how to handle GraphQL errors by Ferry. To simplify let's say that I have toLogin function:

await toLogin(_email, _password);

and it initiates login procedure:

Future<void> sendLogin(String _email, String _password) async {
  final _sendLoginRequest = GLoginReq((b) => b
    ..vars.identifier = _email
    ..vars.password = _password);
  final _client = initClient();
  final _storage = GetStorage();

  _client.request(_sendLoginRequest).listen((response) {
    if (response.hasErrors) {
      print('error');
    } else {
      print('success');
    }
  });
}

Works fine but I need to add error handling at the main program.

so I add

String _result = await toLogin(_email, _password);

to the main and return to the toLogin();

Future<String> sendLogin(String _email, String _password) async {
  final _sendLoginRequest = GLoginReq((b) => b
    ..vars.identifier = _email
    ..vars.password = _password);
  final _client = initClient();

  String _result = 'init';

  _client.request(_sendLoginRequest).listen((response) {
    if (response.hasErrors) {
      print('error');
      _result = 'error';
    } else {
      print('success');
      _result = 'success';
    }
  });
  return _result;
}

The problem is that it returns _result before any check and Dart do not allow me to use await OK, let's try another way:

var _response = OperationResponse(
        operationRequest: _sendLoginRequest,
      );
 
var _checkErrors = _response.graphqlErrors;
print(_checkErrors);

But it's always false I've found option graphqlErrors at OperationResponse but do not have any idea what to fill there.

Please give me a clue how to handle it. I use Strapi as the backend, the error example is as follows:

{
  "errors": [
    {
      "message": "Bad Request",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "login"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "code": 400,
          "data": {
            "statusCode": 400,
            "error": "Bad Request",
            "message": [
              {
                "messages": [
                  {
                    "id": "Auth.form.error.invalid",
                    "message": "Identifier or password invalid."
                  }
                ]
              }
            ],
            "data": [
              {
                "messages": [
                  {
                    "id": "Auth.form.error.invalid",
                    "message": "Identifier or password invalid."
                  }
                ]
              }
            ]
          },
          "stacktrace": [
            "Error: Bad Request",
            "    at checkBadRequest (C:\\Projects\\Zhugory\\zhugory-strapi-graphql\\zhugory-strapi\\node_modules\\strapi-plugin-users-permissions\\config\\schema.graphql.js:13:23)",
            "    at resolver (C:\\Projects\\Zhugory\\zhugory-strapi-graphql\\zhugory-strapi\\node_modules\\strapi-plugin-users-permissions\\config\\schema.graphql.js:217:11)",
            "    at runMicrotasks (<anonymous>)",
            "    at processTicksAndRejections (internal/process/task_queues.js:93:5)"
          ]
        }
      }
    }
  ],
  "data": null
}
0 Answers
Related