refactor(swarm-queue-client): typed errors for the bucket and the guard
Finishes the anyhow removal for the parts this branch adds: the status bucket's open-or-create and the connected-client precondition. Two variants, one of them behind the `kv` feature because the error type it wraps does not exist without it — the error enum respects the same gate the module does. NotConnected is deliberately distinct from Connect: one is a connect that was attempted and refused, the other is a request made before any connection exists. The first is a deployment problem and the second is a caller-ordering one, which is the whole reason a caller wants an enum rather than a string. The controller's `store` now returns the queue client's error rather than an anyhow one: `OnceCell::get_or_try_init` takes its error type from the closure, so widening there would mean converting inside the closure for no gain. `view` `?`s it and anyhow converts at that boundary — the library keeps a typed error, the binary keeps anyhow, and no call site pays for the split.
This commit is contained in:
parent
c028b2ecfc
commit
3273971328
3 changed files with 33 additions and 6 deletions
|
|
@ -19,7 +19,7 @@
|
|||
//! responder, still pulls neither `jetstream` nor `kv`: it speaks the
|
||||
//! connect and nothing else.
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use crate::Error;
|
||||
|
||||
/// The KV bucket hives publish their status snapshots into, one key per
|
||||
/// hive keyed by `hiveName`.
|
||||
|
|
@ -41,7 +41,7 @@ pub const BUCKET: &str = "hive-status";
|
|||
/// into a permanent, silent absence of data.
|
||||
pub async fn open_or_create(
|
||||
client: &async_nats::Client,
|
||||
) -> Result<async_nats::jetstream::kv::Store> {
|
||||
) -> Result<async_nats::jetstream::kv::Store, Error> {
|
||||
let js = async_nats::jetstream::new(client.clone());
|
||||
match js.get_key_value(BUCKET).await {
|
||||
Ok(store) => Ok(store),
|
||||
|
|
@ -58,7 +58,10 @@ pub async fn open_or_create(
|
|||
..Default::default()
|
||||
})
|
||||
.await
|
||||
.with_context(|| format!("creating the {BUCKET} bucket"))
|
||||
.map_err(|source| Error::CreateBucket {
|
||||
bucket: BUCKET,
|
||||
source,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue