Skip to content

Commit

Permalink
Remove tokio thread spawn & improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
conaticus committed Jul 1, 2023
1 parent e6a21fd commit 0672c89
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 20 deletions.
1 change: 1 addition & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ serde_bencode = "0.2.3"
zstd = "0.12.3"
lazy_static = "1.4.0"
open = "5.0.0"
thiserror = "1.0.40"

[features]
# this feature is used for production builds or when `devPath` points to the filesystem
Expand Down
16 changes: 16 additions & 0 deletions src-tauri/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error(transparent)]
Io(#[from] std::io::Error),
#[error("{0}")]
Custom(String),
}

impl serde::Serialize for Error {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
{
serializer.serialize_str(self.to_string().as_ref())
}
}
31 changes: 15 additions & 16 deletions src-tauri/src/filesystem/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
pub mod cache;
pub mod volume;

use std::fmt::Debug;
use crate::filesystem::volume::DirectoryChild;
use std::fs::read_dir;
use std::io;
use std::io::{ErrorKind, Error};
use std::process::ExitStatus;
use crate::errors::Error;

pub const DIRECTORY: &str = "directory";
pub const FILE: &str = "file";
Expand All @@ -18,20 +15,22 @@ pub const fn bytes_to_gb(bytes: u64) -> u16 {
/// Opens a file at the given path. Returns a string if there was an error.
// NOTE(conaticus): I tried handling the errors nicely here but Tauri was mega cringe and wouldn't let me nest results in async functions, so used string error messages instead.
#[tauri::command]
pub async fn open_file(path: String) -> Result<String, ()> {
Ok(tokio::task::spawn_blocking(move || {
let output_res = open::commands(path)[0].output();
let output = match output_res {
Ok(output) => output,
Err(err) => return format!("Failed to get open command output: {}", err)
};

if output.status.success() {
return String::new();
pub async fn open_file(path: String) -> Result<(), Error> {
let output_res = open::commands(path)[0].output();
let output = match output_res {
Ok(output) => output,
Err(err) => {
let err_msg = format!("Failed to get open command output: {}", err);
return Err(Error::Custom(err_msg));
}
};

if output.status.success() {
return Ok(());
}

String::from_utf8(output.stderr).unwrap_or(String::from("Failed to open file and deserialize stderr."))
}).await.unwrap_or(String::from("Failed to create tokio thread when opening file.")))
let err_msg = String::from_utf8(output.stderr).unwrap_or(String::from("Failed to open file and deserialize stderr."));
Err(Error::Custom(err_msg))
}

/// Searches and returns the files in a given directory. This is not recursive.
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

mod filesystem;
mod search;
mod errors;

use filesystem::{open_directory, open_file};
use filesystem::volume::get_volumes;
Expand Down
5 changes: 1 addition & 4 deletions src/components/MainBody/DirectoryContents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ interface Props {

export function DirectoryContents({content, onDirectoryClick}: Props) {
async function onFileClick(path: string) {
const err = await openFile(path);
if (err.length != 0) {
alert(err);
}
await openFile(path).catch(err => alert(err));
}

return <>
Expand Down

0 comments on commit 0672c89

Please sign in to comment.