Robert Estivill
Robert Estivill

Reputation: 12477

Flutter Web: How to get XMLHttpRequest error details?

I'm working on a Flutter web application and I'm having problems handling low level errors properly.

The application uses graphql-flutter, and it seems that any error is wrapped in a library OperationException.

enter image description here

If you look closely, the exception contains a LinkException, that itself contains a originalException and that is a XMLHttpRequest error. No more information is provided here.

Looking at the same from the browser development tools, you can see it's an ERR_CONNECTION_REFUSED because I purposely configured an incorrect URL to trigger this error.

enter image description here

Ideally, I should be able to discriminate and handle connection exceptions, from http errors, from graphql errors in different ways.

To some extent, I was able to at least handle some http status codes for authentication purposes by inspecting the exception

bool isGraphqlResultUnauthorized(QueryResult result) {
  if (result.hasException) {
    if (result.exception?.linkException is HttpLinkParserException) {
      final httpException =
          result.exception?.linkException as HttpLinkParserException;
      if (httpException.response.statusCode == 401) {
        return true;
      }
    }
  }
  return false;
}

All this being said, I'm stuck catching low level errors like timeouts, unknown hosts, connections refuses, etc, and have failed to find a way to retrieve more meaningful information about what's the real cause of the exception.

I would appreciate any tips on how to retrieve this information, as I don't seem to see a way to do it at the moment.

Thank you everyone

Upvotes: 1

Views: 250

Answers (2)

Heitor
Heitor

Reputation: 612

This is a CORS issue. Take a look at this

For local development, you can disable security (note that this is not a recommended solution for production):

flutter run --web-browser-flag "--disable-web-security"

Upvotes: 0

Randal Schwartz
Randal Schwartz

Reputation: 44056

Sadly, XHR was set up merely to provide a limited way for a browser to have "dynamic refresh". I'm amazed at how much we've repurposed it to create full webapps, but we're still out there beyond the original specifications.

That's not really an answer. Fixing many of the problems we have with Flutter web apps acting as a client themselves (like CORS) will require a shift in the powers we give browsers, but that will also lead to more attack vectors for bad actors.

Upvotes: 0

Related Questions