The `cupertino_http` package is throwing `NSErrorClientException` [0] instead
of `HandShakeException` when the certificate is not valid.

Due to this the exception was directly shown in the UI. Inorder to make
the error more user friendly catch the `NSErrorClientException` and
rethrow `HandShakeException` if the certificate is not valid.

- [0] 
https://github.com/dart-lang/http/blob/e0dadd16e01bb4611036f4946ed480dac2d59dca/pkgs/cupertino_http/lib/src/cupertino_client.dart#L219C21-L219C43

Signed-off-by: Shan Shaji <s.sh...@proxmox.com>
---
 changes since v2: 
 - Fixed commit message. 

 lib/src/authenticate.dart | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/lib/src/authenticate.dart b/lib/src/authenticate.dart
index a142a4c..04f363f 100644
--- a/lib/src/authenticate.dart
+++ b/lib/src/authenticate.dart
@@ -2,12 +2,12 @@ import 'dart:async';
 import 'dart:convert';
 import 'dart:io';
 
+import 'package:cupertino_http/cupertino_http.dart';
 import 'package:http/http.dart' as http;
 import 'package:proxmox_dart_api_client/proxmox_dart_api_client.dart';
 import 'package:proxmox_dart_api_client/src/handle_ticket_response.dart';
 import 'package:proxmox_dart_api_client/src/models/serializers.dart';
 
-
 /// Returns an authenticated client to work with if successful.
 ///
 /// This is the function you are looking for if you want to interact with any
@@ -62,6 +62,12 @@ Future<ProxmoxApiClient> authenticate(
       throw HandshakeException(e.message);
     }
     rethrow;
+  } on NSErrorClientException catch (e) {
+    // Treat untrusted server certificate (-1202) as a handshake failure.
+    if (e.error.code == -1202) {
+      throw HandshakeException(e.message);
+    }
+    rethrow;
   }
 }
 
@@ -86,5 +92,11 @@ Future<List<PveAccessDomainModel?>> accessDomains(
       throw HandshakeException(e.message);
     }
     rethrow;
+  } on NSErrorClientException catch (e) {
+    // Treat untrusted server certificate (-1202) as a handshake failure.
+    if (e.error.code == -1202) {
+      throw HandshakeException(e.message);
+    }
+    rethrow;
   }
 }
-- 
2.50.1



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

Reply via email to