Skip to main content

ma_core/
error.rs

1//! Error types for ma-core.
2
3use thiserror::Error;
4
5// ─── Primitive DID/message errors (from ma-did) ─────────────────────────────
6
7pub type MaResult<T> = std::result::Result<T, MaError>;
8
9#[derive(Debug, Error)]
10pub enum MaError {
11    #[error("empty DID")]
12    EmptyDid,
13    #[error("invalid DID prefix, expected did:ma:")]
14    InvalidDidPrefix,
15    #[error("missing DID identifier")]
16    MissingIdentifier,
17    #[error("missing DID fragment")]
18    MissingFragment,
19    #[error("unexpected DID fragment")]
20    UnexpectedFragment,
21    #[error("invalid DID format")]
22    InvalidDidFormat,
23    #[error("invalid DID fragment: {0}")]
24    InvalidFragment(String),
25    #[error("invalid DID identifier")]
26    InvalidIdentifier,
27    #[error("invalid message id")]
28    InvalidMessageId,
29    #[error("empty message id")]
30    EmptyMessageId,
31    #[error("invalid message type")]
32    InvalidMessageType,
33    #[error("invalid key type")]
34    InvalidKeyType,
35    #[error("invalid identity secret")]
36    InvalidIdentitySecret,
37    #[error("invalid recipient")]
38    InvalidRecipient,
39    #[error("missing message content")]
40    MissingContent,
41    #[error("missing message content type")]
42    MissingContentType,
43    #[error("missing sender")]
44    MissingSender,
45    #[error("missing signature")]
46    MissingSignature,
47    #[error("message timestamp is invalid")]
48    InvalidMessageTimestamp,
49    #[error("message is too old")]
50    MessageTooOld,
51    #[error("message timestamp is too far in the future")]
52    MessageFromFuture,
53    #[error("replay detected")]
54    ReplayDetected,
55    #[error("broadcast messages must not have a recipient")]
56    BroadcastMustNotHaveRecipient,
57    #[error("encrypted messages require a recipient")]
58    MessageRequiresRecipient,
59    #[error("context missing")]
60    EmptyContext,
61    #[error("controller missing")]
62    EmptyController,
63    #[error("verification method missing type")]
64    VerificationMethodMissingType,
65    #[error("unknown verification method: {0}")]
66    UnknownVerificationMethod(String),
67    #[error("public key multibase is empty")]
68    EmptyPublicKeyMultibase,
69    #[error("invalid public key multibase")]
70    InvalidPublicKeyMultibase,
71    #[error("invalid multicodec, expected {expected}, got {actual}")]
72    InvalidMulticodec { expected: u64, actual: u64 },
73    #[error("invalid key length, expected {expected}, got {actual}")]
74    InvalidKeyLength { expected: usize, actual: usize },
75    #[error("proof is missing")]
76    MissingProof,
77    #[error("document signature is invalid")]
78    InvalidDocumentSignature,
79    #[error("message signature is invalid")]
80    InvalidMessageSignature,
81    #[error("invalid createdAt timestamp: {0}")]
82    InvalidCreatedAt(String),
83    #[error("invalid updatedAt timestamp: {0}")]
84    InvalidUpdatedAt(String),
85    #[error("identity CID is invalid")]
86    InvalidIdentity,
87    #[error("missing envelope field: {0}")]
88    MissingEnvelopeField(&'static str),
89    #[error("invalid ephemeral key length")]
90    InvalidEphemeralKeyLength,
91    #[error("ciphertext too short")]
92    CiphertextTooShort,
93    #[error("cryptographic operation failed")]
94    Crypto,
95    #[error("CBOR encode failed: {0}")]
96    CborEncode(String),
97    #[error("CBOR decode failed: {0}")]
98    CborDecode(String),
99    #[error("JSON encode failed: {0}")]
100    JsonEncode(String),
101    #[error("JSON decode failed: {0}")]
102    JsonDecode(String),
103}
104
105// ─── Service-level errors ────────────────────────────────────────────────────
106
107/// Errors returned by ma-core public APIs.
108#[derive(Debug, Error)]
109pub enum Error {
110    // ─── Transport ──────────────────────────────────────────────────────
111    #[error("transport error: {0}")]
112    Transport(String),
113
114    #[error("transport connect failed: {0}")]
115    Connect(String),
116
117    #[error("transport bind failed: {0}")]
118    Bind(String),
119
120    #[error("stream open failed: {0}")]
121    StreamOpen(String),
122
123    #[error("connection closed: {0}")]
124    ConnectionClosed(String),
125
126    // ─── Validation ─────────────────────────────────────────────────────
127    #[error("message validation failed: {0}")]
128    Validation(#[from] MaError),
129
130    #[error("message signature verification failed")]
131    SignatureVerification,
132
133    #[error("replay detected for message {0}")]
134    Replay(String),
135
136    // ─── Resolution ─────────────────────────────────────────────────────
137    #[error("DID resolution failed for {did}: {detail}")]
138    Resolution { did: String, detail: String },
139
140    #[error("no inbox transport in DID document for {0}")]
141    NoInboxTransport(String),
142
143    #[error("invalid transport string: {0}")]
144    InvalidTransport(String),
145
146    // ─── Identity / key bootstrap ───────────────────────────────────────
147    #[error("secret key error: {0}")]
148    SecretKey(String),
149
150    #[error("endpoint ID derivation failed: {0}")]
151    EndpointId(String),
152
153    // ─── Config ─────────────────────────────────────────────────────────
154    #[cfg(feature = "config")]
155    #[error("config error: {0}")]
156    Config(String),
157
158    // ─── Secrets bundle ──────────────────────────────────────────────────
159    #[cfg(feature = "config")]
160    #[error("secrets error: {0}")]
161    Secrets(String),
162
163    // ─── ACL ─────────────────────────────────────────────────────────────
164    #[cfg(feature = "acl")]
165    #[error("acl error: {0}")]
166    Acl(String),
167
168    // ─── Service registration ───────────────────────────────────────────
169    #[error("duplicate service ALPN: {0}")]
170    DuplicateService(String),
171
172    // ─── Generic pass-through ───────────────────────────────────────────
173    #[error(transparent)]
174    Other(#[from] anyhow::Error),
175}
176
177pub type Result<T> = std::result::Result<T, Error>;