Skip to main content

spacedb_sdk/
error.rs

1//! SDK errors — the things that stop an op before it runs. Note that a *stale*
2//! read or an *unavailable* strong write are not errors: they are honest
3//! [`spacedb_consistency::Outcome`]s the op returns. Errors are for "you may not"
4//! and "you can't afford it" and "that's not how this field is shaped".
5
6use spacedb_access::DenyReason;
7use thiserror::Error;
8
9use crate::schema::CrdtType;
10
11pub type SdkResult<T> = Result<T, SdkError>;
12
13#[derive(Debug, Error)]
14pub enum SdkError {
15    /// mID authorization refused the op.
16    #[error("access denied: {0:?}")]
17    Denied(DenyReason),
18
19    /// The agent's budget can't cover the op; nothing was charged or written.
20    #[error("over budget: op costs {cost} micro-$MATA, {remaining} remaining")]
21    OverBudget { cost: u64, remaining: u64 },
22
23    /// The op doesn't match the field's CRDT type (e.g. incrementing a register).
24    #[error("field '{field}' is a {found}, not a {expected}")]
25    WrongType {
26        field: String,
27        expected: CrdtType,
28        found: CrdtType,
29    },
30
31    /// A strong-tier field must be written via `claim_unique`, not a plain put.
32    #[error("field '{0}' is strong-tier; use claim_unique")]
33    StrongFieldNeedsClaim(String),
34
35    /// No schema is registered for this collection.
36    #[error("unknown collection '{0}'")]
37    UnknownCollection(String),
38
39    /// The collection has no such field in its schema.
40    #[error("unknown field '{field}' in collection '{collection}'")]
41    UnknownField { collection: String, field: String },
42
43    /// An underlying CRDT operation failed.
44    #[error("crdt error: {0}")]
45    Crdt(String),
46
47    /// An underlying authorization machinery error (not a plain deny).
48    #[error("auth error: {0}")]
49    Auth(String),
50}
51
52impl std::fmt::Display for CrdtType {
53    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54        f.write_str(self.name())
55    }
56}