Skip to content

Commit 957aabe

Browse files
authored
fix: Raise and report errors on connect. (#65)
* For native transports, raise errors for various conditions and properly alert them from the UI when it happens.
1 parent eeed212 commit 957aabe

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

src-tauri/src/transport/gatt.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,30 @@ pub async fn gatt_connect(
1818
id: String,
1919
app_handle: AppHandle,
2020
state: State<'_, super::commands::ActiveConnection<'_>>,
21-
) -> Result<bool, ()> {
22-
let adapter = Adapter::default().await.ok_or(())?;
21+
) -> Result<bool, String> {
22+
let adapter = Adapter::default().await.ok_or("Failed to access the BT adapter".to_string())?;
2323

24-
adapter.wait_available().await.map_err(|_| ())?;
24+
adapter.wait_available().await.map_err(|e| format!("Failed to wait for the BT adapter access: {}", e.message()))?;
2525

2626
let device_id: DeviceId = serde_json::from_str(&id).unwrap();
27-
let d = adapter.open_device(&device_id).await.map_err(|_| ())?;
27+
let d = adapter.open_device(&device_id).await.map_err(|e| format!("Failed to open the device: {}", e.message()))?;
2828

2929
if !d.is_connected().await {
30-
adapter.connect_device(&d).await.map_err(|_| ())?;
30+
adapter.connect_device(&d).await.map_err(|e| format!("Failed to connect to the device: {}", e.message()))?;
3131
}
3232

3333
let service = d
3434
.discover_services_with_uuid(SVC_UUID)
3535
.await
36-
.map_err(|e| ())?
36+
.map_err(|e| format!("Failed to find the device services: {}", e.message()))?
3737
.get(0)
3838
.cloned();
3939

4040
if let Some(s) = service {
4141
let char = s
4242
.discover_characteristics_with_uuid(RPC_CHRC_UUID)
4343
.await
44-
.map_err(|_| ())?
44+
.map_err(|e| format!("Failed to find the studio service characteristics: {}", e.message()))?
4545
.get(0)
4646
.cloned();
4747

@@ -95,10 +95,10 @@ pub async fn gatt_connect(
9595

9696
Ok(true)
9797
} else {
98-
Err(())
98+
Err("Failed to connect: Unable to locate the required studio GATT characteristic".to_string())
9999
}
100100
} else {
101-
Err(())
101+
Err("Failed to connect: Unable to locate the required studio GATT service".to_string())
102102
}
103103
}
104104

src-tauri/src/transport/serial.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub async fn serial_connect(
1515
id: String,
1616
app_handle: AppHandle,
1717
state: State<'_, super::commands::ActiveConnection<'_>>,
18-
) -> Result<bool, ()> {
18+
) -> Result<bool, String> {
1919
match tokio_serial::new(id, 9600).open_native_async() {
2020
Ok(mut port) => {
2121
#[cfg(unix)]
@@ -62,7 +62,7 @@ pub async fn serial_connect(
6262
Ok(true)
6363
}
6464
Err(e) => {
65-
Err(())
65+
Err(format!("Failed to open the serial port: {}", e.description))
6666
}
6767
}
6868
}

src/ConnectModal.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ function deviceList(
7777
}
7878
const dev = devices.find(([_t, d]) => keys.has(d.id));
7979
if (dev) {
80-
onTransportCreated(await dev[0].pick_and_connect!.connect(dev[1]));
80+
dev[0]
81+
.pick_and_connect!.connect(dev[1])
82+
.then(onTransportCreated)
83+
.catch((e) => alert(e));
8184
}
8285
},
8386
[devices, onTransportCreated]

0 commit comments

Comments
 (0)