2

I'm attempting to detect whether a connection refused error is taking place with the following tutorial: https://www.dartlang.org/dart-by-example/#http-requests-and-responses ; However, I continue to receive the method not found exception and cannot receive any feedback whether my HTTP Response was working or not. Any ideas would be appreciated. Please let me know if you need more information. --ignore "shroot".

handleFailure(error) {
    print('Something went wrong.');
    print(error.message);
  }

  void loadData(String url) {

    //url = "${baseRestfulUrl}"+ QUERY_ALL_TARGET_ASSETS_BASE_URL;
    print(url);
    //call the web server asynchronously
    var request = HttpRequest.getString(url).then(onDataLoaded);
      request.catchError(handleFailure);

    if (request == null) {
      var warn = (shroot.querySelector('#warning')
          ..text = "No records could be found to match the search criteria."
          ..style.color = "Red");

      ButtonElement resetBtn = shroot.querySelector("#reset-btn");
      resetBtn.disabled = true; 

    } 
    else if(request == 404)
    {
      var warn = (shroot.querySelector('#warning')
               ..text = "Error serving data.  Please restart server."
               ..style.color = "Red");
    }
    else if(request == "ERR_CONNECTION_REFUSED")
    {
      var warn = (shroot.querySelector('#warning')
                    ..text = "Error serving data.  Please restart server."
                    ..style.color = "Red");
    }
    else if(request != null)
    {
      var warn = (shroot.querySelector('#warning')
               ..text = ""
               ..style.color = "#fff");

      ButtonElement resetBtn = shroot.querySelector("#reset-btn");
            resetBtn.disabled = false;
    }
    else {
      var warn = (shroot.querySelector('#warning')
                          ..text = "Error serving data.  Please restart server."
                          ..style.color = "Red");

      ButtonElement resetBtn = shroot.querySelector("#reset-btn");
      resetBtn.disabled = false; 
    }
  }

1 Answer 1

1

The link you added is about Dart I/O and Command Line Apps. This doesn't apply to Dart in the browser. Dart has to delegate to the browser API with limited capabilities and therefore there are differences.

In the case of an error an instance of ProgressEvent is passed to this method

handleFailure(error) {
  print('Something went wrong.');
  // print(error.message); // ProgressEvent doesn't have a `message` getter
  // the progress event doesn't provide any further information

  if(error is ProgressEvent && (error as ProgressEvent).type == 'error') {
    print('An error has occured'); // as we already know because `handleFailure()` was called
  }
}

this code

if (request == null) {

is actually called before the request was sent because it is not inside a .then

var request = HttpRequest.getString(url).then(onDataLoaded);
request.catchError(handleFailure)
.then((e) {
  if(request == null) { // doesn't make sense because you just assigned a value to request
    ...
  }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.