Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Raise and report errors on connect. #65

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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