I am creating an app using Flutter, with the intention to publish it as a web app (for use in a web browser instead of Android or iOS). Whenever I attempt to run an HTTP request against a URL that does not exist (usually because my local server is not running), I get this error:
Error: XMLHttpRequest error.
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 299:10 createErrorWithStack
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 341:28 _throw
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/core/errors.dart 116:5 throwWithStackTrace
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/zone.dart 1378:11 callback
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/schedule_microtask.dart 40:11 _microtaskLoop
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/schedule_microtask.dart 49:5 _startMicrotaskLoop
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 166:15 <fn>
I definitely expect some kind of error, since the server is not running. But, this doesn't give any details about the error. Also, whenever I use a try-catch, the data from the catch simply says:
XMLHTTPRequest error.
This is not helpful for someone with less knowledge of the app.
Importantly, whenever there is any other kind of server error (such as 400 Bad Request), it returns the exact same error. This is fine when testing, where I can just check the server logs to see the HTTP status code, but in production, I want users to be able to report the error message they see, so I have a better idea of the cause, so I can fix it.
Is there a way to configure Flutter (or Dart) to use the actual HTTP response instead of just throwing an XMLHTTPRequest error every time, or provide more details on whatever error is occurring?
I am using the simple http package:
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
final encodedBody = json.encode(body);
final response = await http.post(
Uri.parse(uri),
headers: sendHeaders,
body: encodedBody,
);
I am testing using Android Studio, if that helps.
Thanks in advance!