tauri-apps / tauri-apps/plugins-workspace
[store] Error handling in the `with_store` function
- Dominant language
- Rust
- Stars
- 1.8k
- Forks
- 602
- Avg merge
- 4d 14h
- Merged PRs (30d)
- 9
Description
The `with_store` function expects the last argument, the callback, to return a `Result`, where `Error` is a built-in error type. It's rather restricting because it doesn't allow for meaningful error handling on the user side.
For example (doesn't compile):
```rust
let value = tauri_plugin_store::with_store(
self.handle,
self.stores,
SETTINGS_PATH.into(),
|store| {
store
.get(key)
.and_then(|value| value.as_u64())
.or_else(|| self.defaults.get(key).and_then(|value| value.as_u64()))
},
);
```
This callback returns `Option`, but the `with_store` function expects it to return `Result`. I want to convert this `Option` into an error using `.ok_or()`, but I need a meaningful way to communicate that a value with a given key was not found in the store. None of the options of the `Error` enum satisfy that purpose:
```rust
pub enum Error {
#[error("Failed to serialize store. {0}")]
Serialize(Box),
#[error("Failed to deserialize store. {0}")]
Deserialize(Box),
/// JSON error.
#[error(transparent)]
Json(#[from] serde_json::Error),
/// IO error.
#[error(transparent)]
Io(#[from] std::io::Error),
/// Store not found
#[error("Store \"{0}\" not found")]
NotFound(PathBuf),
/// Some Tauri API failed
#[error(transparent)]
Tauri(#[from] tauri::Error),
}
```
Contributor guide
Assessment
This issue has not been assessed yet.