Skip to content

Commit

Permalink
fix: Raise and report errors on connect.
Browse files Browse the repository at this point in the history
* For native transports, raise errors for various conditions and
  properly alert them from the UI when it happens.
  • Loading branch information
petejohanson committed Oct 16, 2024
1 parent eeed212 commit 2b8980c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
18 changes: 9 additions & 9 deletions src-tauri/src/transport/gatt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,30 @@ pub async fn gatt_connect(
id: String,
app_handle: AppHandle,
state: State<'_, super::commands::ActiveConnection<'_>>,
) -> Result<bool, ()> {
let adapter = Adapter::default().await.ok_or(())?;
) -> Result<bool, String> {
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();

if let Some(s) = service {
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();

Expand Down Expand Up @@ -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())
}
}

Expand Down
4 changes: 2 additions & 2 deletions src-tauri/src/transport/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub async fn serial_connect(
id: String,
app_handle: AppHandle,
state: State<'_, super::commands::ActiveConnection<'_>>,
) -> Result<bool, ()> {
) -> Result<bool, String> {
match tokio_serial::new(id, 9600).open_native_async() {
Ok(mut port) => {
#[cfg(unix)]
Expand Down Expand Up @@ -62,7 +62,7 @@ pub async fn serial_connect(
Ok(true)
}
Err(e) => {
Err(())
Err(format!("Failed to open the serial port: {}", e.description))
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/ConnectModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 2b8980c

Please sign in to comment.