I'm creating a tauri app. I want to call a command with some data (see code snippets below). But when I do I get the following error. This is weird since the field is provided as you can see (and tauri changes it to snake_case in the background). Could this have another cause?
ERROR invalid args `data` for command `decrypt_local_database_with_pin`: missing field `cause_error`
I should note that the property in question
cause_errorand thethread.sleepcall are there to help me test this application until the backend is ready. So there is no real functionality, yet.
These are the code parts in question:
The Rust Part
#[derive(Debug, thiserror::Error)]
pub enum CommandError {
#[error(transparent)]
Error(#[from] anyhow::Error),
}
impl Serialize for CommandError {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,
{
serializer.serialize_str(self.to_string().as_ref())
}
}
pub type CommandResult<T, E = CommandError> = anyhow::Result<T, E>;
#[derive(Debug, Serialize, Deserialize)]
pub struct CommandValue<T> {
pub cause_error: bool,
pub values: T
}
#[derive(Debug, Deserialize)]
pub struct DecryptLocalDatabaseWithPinData {
pub pin: String,
}
#[tauri::command]
pub fn decrypt_local_database_with_pin(data: CommandValue<DecryptLocalDatabaseWithPinData>) -> CommandResult<String> {
thread::sleep(Duration::from_millis(2000));
match data.cause_error {
true => Ok(format!("Local database decrypted with pin: {}", data.values.pin)),
false => Err(CommandError::Error(anyhow::anyhow!(format!("Local database was not decrypted with pin: {}", data.values.pin)))),
}
}
The Typescript Part
encryptLocalDatabaseWithPin(pin: string) {
return invoke("decrypt_local_database_with_pin", {
data: { pin },
causeError: true,
});
}
You have structured your types and arguments wrong. With the Tauri command you've specified, you'd need to call it like this:
And thus the error is correct: you provided
cause_error, but in the wrong place.Additionally:
valuesbecause that is how your Rust types are built, you can use#[serde(flatten)]to avoid that if you wish.cause_errorshould be in snake_case; Tauri will translate the argument names between snake_case and camelCase, but not the contents of the values passed. Right nowdatais the only "argument" and what it holds will be un-touched. You can use#[serde(rename_all = "camelCase")]to avoid that if you wish.If you were to want
causeErrorto work as you tried, it would have to be a separate parameter on the command: