Skip to main content

aven_core/sync/client/
errors.rs

1//! Stable error codes carried by client errors.
2//!
3//! Every engine error message starts `error <code>`, optionally followed by
4//! details and a `hint="..."`. Hosts map codes, never message text.
5use anyhow::Error;
6
7/// The stable error codes in the chain, outermost first.
8pub fn codes(error: &Error) -> impl Iterator<Item = String> + '_ {
9    error.chain().filter_map(|cause| {
10        cause
11            .to_string()
12            .strip_prefix("error ")
13            .and_then(|rest| rest.split_whitespace().next())
14            .map(str::to_string)
15    })
16}
17
18/// The outermost stable error code in the chain.
19pub fn code(error: &Error) -> Option<String> {
20    codes(error).next()
21}
22
23pub fn has_code(error: &Error, expected: &str) -> bool {
24    codes(error).any(|code| code == expected)
25}
26
27/// The server saw an older membership head than its current one.
28pub fn is_stale(error: &Error) -> bool {
29    error.is::<crate::sync::seed_claim::membership::StaleContext>()
30}
31
32/// The server refused this device's credential or membership, which may
33/// mean another device removed it.
34pub fn is_access_refusal(error: &Error) -> bool {
35    has_code(error, "sync-server-refused")
36        || has_code(error, "enrollment-unauthorized")
37        || has_code(error, "enrollment-revoked")
38        || has_code(error, "sync-device-removed")
39}