From 2b8980c587e39e3c1e9bce7b1857cb97cf8e4872 Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Wed, 16 Oct 2024 13:58:17 -0600 Subject: [PATCH] fix: Raise and report errors on connect. * For native transports, raise errors for various conditions and properly alert them from the UI when it happens. --- src-tauri/src/transport/gatt.rs | 18 +++++++++--------- src-tauri/src/transport/serial.rs | 4 ++-- src/ConnectModal.tsx | 5 ++++- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src-tauri/src/transport/gatt.rs b/src-tauri/src/transport/gatt.rs index 0b68565..357fc25 100644 --- a/src-tauri/src/transport/gatt.rs +++ b/src-tauri/src/transport/gatt.rs @@ -18,22 +18,22 @@ pub async fn gatt_connect( id: String, app_handle: AppHandle, state: State<'_, super::commands::ActiveConnection<'_>>, -) -> Result { - let adapter = Adapter::default().await.ok_or(())?; +) -> Result { + let adapter = Adapter::default().await.ok_or("Failed to access the BT adapter".to_string())?; - adapter.wait_available().await.map_err(|_| ())?; + adapter.wait_available().await.map_err(|e| format!("Failed to wait for the BT adapter access: {}", e.message()))?; let device_id: DeviceId = serde_json::from_str(&id).unwrap(); - let d = adapter.open_device(&device_id).await.map_err(|_| ())?; + let d = adapter.open_device(&device_id).await.map_err(|e| format!("Failed to open the device: {}", e.message()))?; if !d.is_connected().await { - adapter.connect_device(&d).await.map_err(|_| ())?; + adapter.connect_device(&d).await.map_err(|e| format!("Failed to connect to the device: {}", e.message()))?; } let service = d .discover_services_with_uuid(SVC_UUID) .await - .map_err(|e| ())? + .map_err(|e| format!("Failed to find the device services: {}", e.message()))? .get(0) .cloned(); @@ -41,7 +41,7 @@ pub async fn gatt_connect( let char = s .discover_characteristics_with_uuid(RPC_CHRC_UUID) .await - .map_err(|_| ())? + .map_err(|e| format!("Failed to find the studio service characteristics: {}", e.message()))? .get(0) .cloned(); @@ -95,10 +95,10 @@ pub async fn gatt_connect( Ok(true) } else { - Err(()) + Err("Failed to connect: Unable to locate the required studio GATT characteristic".to_string()) } } else { - Err(()) + Err("Failed to connect: Unable to locate the required studio GATT service".to_string()) } } diff --git a/src-tauri/src/transport/serial.rs b/src-tauri/src/transport/serial.rs index eefa4e2..cebba80 100644 --- a/src-tauri/src/transport/serial.rs +++ b/src-tauri/src/transport/serial.rs @@ -15,7 +15,7 @@ pub async fn serial_connect( id: String, app_handle: AppHandle, state: State<'_, super::commands::ActiveConnection<'_>>, -) -> Result { +) -> Result { match tokio_serial::new(id, 9600).open_native_async() { Ok(mut port) => { #[cfg(unix)] @@ -62,7 +62,7 @@ pub async fn serial_connect( Ok(true) } Err(e) => { - Err(()) + Err(format!("Failed to open the serial port: {}", e.description)) } } } diff --git a/src/ConnectModal.tsx b/src/ConnectModal.tsx index 4fa2652..8e29bc3 100644 --- a/src/ConnectModal.tsx +++ b/src/ConnectModal.tsx @@ -77,7 +77,10 @@ function deviceList( } const dev = devices.find(([_t, d]) => keys.has(d.id)); if (dev) { - onTransportCreated(await dev[0].pick_and_connect!.connect(dev[1])); + dev[0] + .pick_and_connect!.connect(dev[1]) + .then(onTransportCreated) + .catch((e) => alert(e)); } }, [devices, onTransportCreated]