specta-rs/specta

Support generate code when Error impl Serialize

Opened this issue · 0 comments

Then can get rid of these

call_something().await.map_err(|e| e.to_string())?;

Tauri official guide (https://tauri.app/v1/guides/features/command/#error-handling)

// create the error type that represents all errors possible in our program
#[derive(Debug, thiserror::Error)]
enum Error {
  #[error(transparent)]
  Io(#[from] std::io::Error)
}

// we must manually implement serde::Serialize
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())
  }
}

#[tauri::command]
fn my_custom_command() -> Result<(), Error> {
  // This will return an error
  std::fs::File::open("path/that/does/not/exist")?;
  // Return nothing on success
  Ok(())
}

I've test TauRPC, and that crate support this. ( https://github.com/MatsDK/TauRPC )