tauri icon indicating copy to clipboard operation
tauri copied to clipboard

[docs] include imports in the documentation

Open firstdorsal opened this issue 3 years ago • 1 comments

Regarding this part of the Documentation: https://tauri.app/v1/guides/features/command/#error-handling

I am trying to return a simple result and handle an Error.

Err("This failed!".into()) does not work and I have no idea what I would need to import to make it work because of the missing use/import statements.

firstdorsal avatar Oct 16 '22 15:10 firstdorsal

There are no missing imports here. The example uses only the std prelude.

Can you share errors and/or relevant code that doesn't work for you?

FabianLars avatar Oct 16 '22 17:10 FabianLars

#![cfg_attr(
    all(not(debug_assertions), target_os = "windows"),
    windows_subsystem = "windows"
)]

use std::fs::DirEntry;

use filez::some_or_bail;

// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[tauri::command]
fn sync(server_url: &str, local_folder: &str, remote_volume: &str) -> tauri::Result<()> {
    println!(
        "Syncing {} to {} on {}",
        local_folder, remote_volume, server_url
    );

    let files = match recursive_read_dir(local_folder) {
        Ok(files) => files,
        Err(e) => return Err("".into()),
    };

    // calculate the hash of each file

    dbg!(files);

    Ok(())
}

pub fn recursive_read_dir(path: &str) -> anyhow::Result<Vec<DirEntry>> {
    let mut entries = Vec::new();
    for entry in std::fs::read_dir(path)? {
        let entry = entry?;
        let path = entry.path();
        if path.is_dir() {
            entries.extend(recursive_read_dir(some_or_bail!(
                path.to_str(),
                "Could not convert path to sr"
            ))?);
        } else {
            entries.push(entry);
        }
    }
    Ok(entries)
}

fn main() {
    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![sync])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

and the return Err("".into()) does not work

the trait bound `tauri::Error: std::convert::From<&str>` is not satisfied
the following other types implement trait `std::convert::From<T>`:
  <tauri::Error as std::convert::From<glob::PatternError>>
  <tauri::Error as std::convert::From<serde_json::error::Error>>
  <tauri::Error as std::convert::From<std::boxed::Box<tauri::ShellScopeError>>>
  <tauri::Error as std::convert::From<std::io::Error>>
  <tauri::Error as std::convert::From<tauri::api::Error>>
  <tauri::Error as std::convert::From<tauri_runtime::Error>>
  <tauri::Error as std::convert::From<tokio::runtime::task::error::JoinError>>
required because of the requirements on the impl of `std::convert::Into<tauri::Error>` for `&str`rustc[E0277](https://doc.rust-lang.org/error-index.html#E0277)

firstdorsal avatar Oct 17 '22 12:10 firstdorsal

So the issue here is that you're using tauri's Result/Error type instead of Rust's standard one, so if you replace -> tauri::Result<()> with -> Result<(), String> like in the examples you linked it should work again.

FabianLars avatar Oct 17 '22 12:10 FabianLars

Ahh ok thank you! It didn't work with just Result<()> like it usually does but it did work with tauri::Result<()> thats why I thought it would be the tauri Result. But yes with Result<(), String> it works.

firstdorsal avatar Oct 17 '22 12:10 firstdorsal