http Post waiting for response that never comes

682 views
Skip to first unread message

Kevin Roberson

unread,
Feb 13, 2021, 2:12:52 PM2/13/21
to Flutter Development (flutter-dev)
Hello,
I'm posting to my node.js api server, which in turn sends the sql insert to the MySQL database.  It works perfectly and I display a success page to the user.  But, if the api server or db server are down, I want to display an error message to the user and send an email to a support account to alert me.  If I end either server to test this, what happens is I get the 'XMLHttpRequest error' and it simply fails with no return.  So, my http.response.statusCode is null and I have no way to check it.  I've tried several solutions, but I can't find a way to look for the 'XMLHttpRequest error'.  I've looked at FutureBuilder.  Would I be able to use that to build my error alert window and then log the error and send the email?  I'm trying to keep the error handling and logging reusable throughout the site.  Here is the error information and the code snippets.  I would welcome any advise on how to architect this.  It seems like it would be pretty straightforward,  but I'm not getting it and not finding many examples.  Thanks!

It fails in Flutter's browser_client.dart completer.completeError here:
unawaited(xhr.onError.first.then((_) {
      // Unfortunately, the underlying XMLHttpRequest API doesn't expose any
      // specific information about the error itself.
      completer.completeError(
          ClientException('XMLHttpRequest error.', request.url),
          StackTrace.current);
    }));

    xhr.send(bytes);

    try {
      return await completer.future;
    } finally {
      _xhrs.remove(xhr);
    }
  }

My console error is:
Launching lib\main.dart on Chrome in debug mode...
 lib\main.dart
Debug service listening on ws://127.0.0.1:54711/Bi8IbBcsGx0=/ws
Running with unsound null safety
Connecting to VM Service at ws://127.0.0.1:54711/Bi8IbBcsGx0=/ws
Error: XMLHttpRequest error.
[38;5;248mC:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 906:28                get current [39;49m
[38;5;248mpackages/http/src/browser_client.dart 84:22                                                                                    <fn> [39;49m
[38;5;248mC:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/zone.dart 1446:54                                              runUnary [39;49m
[38;5;248mC:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 150:18                                        handleValue [39;49m
[38;5;248mC:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 703:44                                        handleValueCallback [39;49m
[38;5;248mC:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 732:32                                        _propagateToListeners [39;49m
[38;5;248mC:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 526:7                                         [_complete] [39;49m
[38;5;248mC:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/stream_pipe.dart 61:11                                         _cancelAndValue [39;49m
[38;5;248mC:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/stream.dart 1302:7                                             <fn> [39;49m
[38;5;248mC:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 324:14  _checkAndCall [39;49m
[38;5;248mC:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 329:39  dcall [39;49m
[38;5;248mC:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/html/dart2js/html_dart2js.dart 37307:58                              <fn> [39;49m
    at Object.createErrorWithStack (http://localhost:54651/dart_sdk.js:4375:12)
    at async._AsyncCallbackEntry.new.callback (http://localhost:54651/dart_sdk.js:38290:13)
    at Object._microtaskLoop (http://localhost:54651/dart_sdk.js:38122:13)
    at _startMicrotaskLoop (http://localhost:54651/dart_sdk.js:38128:13)
Exited (1)

My code attempted is:
Future addUser() async {
    var regForm = new Map();

    regForm['firstName'] = _firstNameTextController.text;
    regForm['lastName'] = _lastNameTextController.text;
    regForm['email'] = _emailTextController.text;
    regForm['relationship'] = _dropdownValue;
    regForm['relationOther'] = _relationTextController.text;
    regForm['username'] = _usernameTextController.text;
    regForm['password'] = encr(_passwordTextController.text);
    regForm['passwordHint'] = _passwordHintTextController.text;
    regForm['passwordQ1'] = _passwordRecoveryQ1TextController.text;
    regForm['passwordA1'] = _passwordRecoveryA1TextController.text;
    regForm['passwordQ2'] = _passwordRecoveryQ2TextController.text;
    regForm['passwordA2'] = _passwordRecoveryA2TextController.text;
    regForm['passwordQ3'] = _passwordRecoveryQ3TextController.text;
    regForm['passwordA3'] = _passwordRecoveryA3TextController.text;

    final response = await http.post(
      Constants.API_URL + '/addUser',
      headers: <String, String>{'Content-Type': 'application/json'},
      body: json.encode(regForm),
    );

    if (response.statusCode == 200) {
      Navigator.of(context).pushNamed(Success.route);
    } else {
      String subject = Messages.ERROR_USER_REGISTRATION_FAILED;
      String error =
          'Error registering user $user.  Response code: ${response.statusCode.toString()}';

      var logger = Logger('_RegisterFormState');  // External function to log error - send class name.

      errorMsg(context, user, subject, error);  // External generic function to display alert window and send email.
    }
  }
Reply all
Reply to author
Forward
0 new messages