In a relatively simple block of code that checks an API endpoint (determining connection state), I rely on a try..catch
as the mechanism to validate if the application can communicate with the server.
The issue I'm having is that while debugging, the debugger always stops on the connection line (when the application is offline) even though I am handling the errors internally.
Future<bool> isOnline() async {
try {
// VSCode debugger always stops on this line when no connection
await http
.get('${consts.apiBaseUrl}/api/ping')
.timeout(Duration(seconds: normalTimeoutLength))
.catchError(
(_) {
// Trying catchError on the Future
_isOnline = false;
return false;
},
);
_isOnline = true;
return true;
} on HttpException catch (_) {
// Trying to catch HTTP Exceptions
_isOnline = false;
return false;
} on SocketException catch (_) {
// Trying to catch Socket Exceptions
_isOnline = false;
return false;
}
}