Skip to content

Commit

Permalink
refactor: improve error handling (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
kasugamirai authored Jun 24, 2024
1 parent 3fbd66d commit f0712e5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 38 deletions.
11 changes: 4 additions & 7 deletions src-tauri/src/fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use tauri_nspanel::{
base::{id, nil},
foundation::{NSPoint, NSRect},
},
ManagerExt,
objc::{class, msg_send, runtime::NO, sel, sel_impl}, panel_delegate, WebviewWindowExt,
objc::{class, msg_send, runtime::NO, sel, sel_impl},
panel_delegate, ManagerExt, WebviewWindowExt,
};

#[allow(non_upper_case_globals)]
Expand All @@ -28,11 +28,8 @@ pub fn swizzle_to_menubar_panel(app_handle: &tauri::AppHandle) {
let handle = app_handle.clone();

panel_delegate.set_listener(Box::new(move |delegate_name: String| {
match delegate_name.as_str() {
"window_did_resign_key" => {
let _ = handle.emit("menubar_panel_did_resign_key", ());
}
_ => (),
if delegate_name.as_str() == "window_did_resign_key" {
let _ = handle.emit("menubar_panel_did_resign_key", ());
}
}));

Expand Down
59 changes: 28 additions & 31 deletions src-tauri/src/nostr/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ pub struct Relays {

#[tauri::command]
#[specta::specta]
pub async fn get_relays(state: State<'_, Nostr>) -> Result<Relays, ()> {
pub async fn get_relays(state: State<'_, Nostr>) -> Result<Relays, String> {
let client = &state.client;

// Get connected relays
let list = client.relays().await;
let connected_relays: Vec<String> = list.into_keys().map(|url| url.to_string()).collect();

// Get NIP-65 relay list
let signer = client.signer().await.unwrap();
let public_key = signer.public_key().await.unwrap();
let signer = client.signer().await.map_err(|e| e.to_string())?;
let public_key = signer.public_key().await.map_err(|e| e.to_string())?;
let filter = Filter::new()
.author(public_key)
.kind(Kind::RelayList)
Expand Down Expand Up @@ -71,53 +71,50 @@ pub async fn get_relays(state: State<'_, Nostr>) -> Result<Relays, ()> {
})
}
}
Err(_) => Ok(Relays {
connected: connected_relays,
read: None,
write: None,
both: None,
}),
Err(e) => Err(e.to_string()),
}
}

#[tauri::command]
#[specta::specta]
pub async fn connect_relay(relay: &str, state: State<'_, Nostr>) -> Result<bool, ()> {
pub async fn connect_relay(relay: &str, state: State<'_, Nostr>) -> Result<bool, String> {
let client = &state.client;
if let Ok(status) = client.add_relay(relay).await {
if status {
println!("connecting to relay: {}", relay);
let _ = client.connect_relay(relay).await;
Ok(true)
} else {
Ok(false)
match client.add_relay(relay).await {
Ok(status) => {
if status {
println!("connecting to relay: {}", relay);
let _ = client.connect_relay(relay).await;
Ok(true)
} else {
Ok(false)
}
}
} else {
Ok(false)
Err(e) => Err(e.to_string()),
}
}

#[tauri::command]
#[specta::specta]
pub async fn remove_relay(relay: &str, state: State<'_, Nostr>) -> Result<bool, ()> {
pub async fn remove_relay(relay: &str, state: State<'_, Nostr>) -> Result<bool, String> {
let client = &state.client;
if (client.remove_relay(relay).await).is_ok() {
let _ = client.disconnect_relay(relay).await;
Ok(true)
} else {
Ok(false)
match client.remove_relay(relay).await {
Ok(_) => {
let _ = client.disconnect_relay(relay).await;
Ok(true)
}
Err(e) => Err(e.to_string()),
}
}

#[tauri::command]
#[specta::specta]
pub fn get_bootstrap_relays(app: tauri::AppHandle) -> Result<Vec<String>, ()> {
pub fn get_bootstrap_relays(app: tauri::AppHandle) -> Result<Vec<String>, String> {
let relays_path = app
.path()
.resolve("resources/relays.txt", BaseDirectory::Resource)
.expect("Bootstrap relays not found.");
.map_err(|e| e.to_string())?;

let file = std::fs::File::open(relays_path).unwrap();
let file = std::fs::File::open(relays_path).map_err(|e| e.to_string())?;
let lines = io::BufReader::new(file).lines();

let mut relays = Vec::new();
Expand All @@ -135,15 +132,15 @@ pub fn save_bootstrap_relays(relays: &str, app: tauri::AppHandle) -> Result<(),
let relays_path = app
.path()
.resolve("resources/relays.txt", BaseDirectory::Resource)
.expect("Bootstrap relays not found.");
.map_err(|e| e.to_string())?;

let mut file = fs::OpenOptions::new()
.write(true)
.open(relays_path)
.unwrap();
.map_err(|e| e.to_string())?;

match file.write_all(relays.as_bytes()) {
Ok(_) => Ok(()),
Err(_) => Err("Cannot save bootstrap relays, please try again later.".into()),
Err(e) => Err(e.to_string()),
}
}

0 comments on commit f0712e5

Please sign in to comment.