plugins-workspace
plugins-workspace copied to clipboard
[store] Error handling in the `with_store` function
The with_store
function expects the last argument, the callback, to return a Result<T, Error>
, 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):
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<u64>
, but the with_store
function expects it to return Result<u64, Error>
. 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:
pub enum Error {
#[error("Failed to serialize store. {0}")]
Serialize(Box<dyn std::error::Error + Send + Sync>),
#[error("Failed to deserialize store. {0}")]
Deserialize(Box<dyn std::error::Error + Send + Sync>),
/// 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),
}