]> git.proxmox.com Git - flutter/proxmox_login_manager.git/commitdiff
login form: restructure _tryGetAccessDomains
authorDominik Csapak <d.csapak@proxmox.com>
Tue, 25 Apr 2023 11:47:16 +0000 (13:47 +0200)
committerDominik Csapak <d.csapak@proxmox.com>
Tue, 25 Apr 2023 11:47:16 +0000 (13:47 +0200)
by splitting it into two functions:
_tryLoadAccessDomains
_loadAccessDomains

where the _try variant never throws an exception, but shows a
ConnectionErrorDialog on failure.

this also reorders the code a bit to not need the 'hasPort' variable
at all

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
lib/proxmox_login_form.dart

index d649dafc711ea9c4f247a3ba8b7535e8ab6c041c..48d1e5adc9781183f23ebcee7ca1f24eba7b7eb4 100644 (file)
@@ -530,8 +530,7 @@ class _ProxmoxLoginPageState extends State<ProxmoxLoginPage> {
     });
   }
 
-  Future<List<PveAccessDomainModel?>?> _tryGetAccessDomains(
-      Uri uri, bool showConnectionError) async {
+  Future<List<PveAccessDomainModel?>?> _loadAccessDomains(Uri uri) async {
     final settings = await ProxmoxGeneralSettingsModel.fromLocalStorage();
     List<PveAccessDomainModel?>? response;
     try {
@@ -543,25 +542,26 @@ class _ProxmoxLoginPageState extends State<ProxmoxLoginPage> {
           exception: e,
         ),
       );
-    } catch (e, trace) {
-      print(e);
-      print(trace);
-      if (e.runtimeType == HandshakeException) {
-        showDialog(
-          context: context,
-          builder: (context) => ProxmoxCertificateErrorDialog(),
-        );
-      } else {
-        if (showConnectionError) {
-          showDialog(
-              context: context,
-              builder: (context) => ConnectionErrorDialog(
-                    exception: e,
-                  ));
-        } else {
-          throw e;
-        }
-      }
+    } on HandshakeException {
+      showDialog(
+        context: context,
+        builder: (context) => ProxmoxCertificateErrorDialog(),
+      );
+    }
+    return response;
+  }
+
+  Future<List<PveAccessDomainModel?>?> _tryLoadAccessDomains(Uri uri) async {
+    List<PveAccessDomainModel?>? response;
+    try {
+      response = await _loadAccessDomains(uri);
+    } catch (e) {
+      showDialog(
+        context: context,
+        builder: (context) => ConnectionErrorDialog(
+          exception: e,
+        ),
+      );
     }
     return response;
   }
@@ -583,24 +583,22 @@ class _ProxmoxLoginPageState extends State<ProxmoxLoginPage> {
 
     RegExp portRE = new RegExp(r":\d{1,5}$");
 
-    bool hasPort = true;
-    if (!portRE.hasMatch(host)) {
-      hasPort = false;
-      apiBaseUrl = apiBaseUrl.replace(port: 8006);
-    }
-
     List<PveAccessDomainModel?>? response;
 
-    try {
-      response = await _tryGetAccessDomains(apiBaseUrl, hasPort);
-      if (!hasPort) {
-        _originController.text = '$host:8006';
-      }
-    } catch (e) {
-      if (!hasPort) {
+    if (portRE.hasMatch(host)) {
+      response = await _tryLoadAccessDomains(apiBaseUrl);
+    } else {
+      // try to guess the port, 8006 first, and then 443
+      apiBaseUrl = apiBaseUrl.replace(port: 8006);
+      try {
+        response = await _loadAccessDomains(apiBaseUrl);
+        if (response != null) {
+          _originController.text = '$host:8006';
+        }
+      } catch (e) {
         // we were no port given, and we couldn't reach on port 8006, retry with 443
         apiBaseUrl = apiBaseUrl.replace(port: 443);
-        response = await _tryGetAccessDomains(apiBaseUrl, true);
+        response = await _tryLoadAccessDomains(apiBaseUrl);
         if (response != null) {
           _originController.text = '$host:443';
         }