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("missing envelope field: {0}")]
86    MissingEnvelopeField(&'static str),
87    #[error("invalid ephemeral key length")]
88    InvalidEphemeralKeyLength,
89    #[error("ciphertext too short")]
90    CiphertextTooShort,
91    #[error("cryptographic operation failed")]
92    Crypto,
93    #[error("CBOR encode failed: {0}")]
94    CborEncode(String),
95    #[error("CBOR decode failed: {0}")]
96    CborDecode(String),
97    #[error("JSON encode failed: {0}")]
98    JsonEncode(String),
99    #[error("JSON decode failed: {0}")]
100    JsonDecode(String),
101}
102
103// ─── Service-level errors ────────────────────────────────────────────────────
104
105/// Errors returned by ma-core public APIs.
106#[derive(Debug, Error)]
107pub enum Error {
108    // ─── Transport ──────────────────────────────────────────────────────
109    #[error("transport error: {0}")]
110    Transport(String),
111
112    #[error("transport connect failed: {0}")]
113    Connect(String),
114
115    #[error("transport bind failed: {0}")]
116    Bind(String),
117
118    #[error("stream open failed: {0}")]
119    StreamOpen(String),
120
121    #[error("connection closed: {0}")]
122    ConnectionClosed(String),
123
124    // ─── Validation ─────────────────────────────────────────────────────
125    #[error("message validation failed: {0}")]
126    Validation(#[from] MaError),
127
128    #[error("message signature verification failed")]
129    SignatureVerification,
130
131    #[error("replay detected for message {0}")]
132    Replay(String),
133
134    // ─── Resolution ─────────────────────────────────────────────────────
135    #[error("DID resolution failed for {did}: {detail}")]
136    Resolution { did: String, detail: String },
137
138    #[error("no inbox transport in DID document for {0}")]
139    NoInboxTransport(String),
140
141    #[error("invalid transport string: {0}")]
142    InvalidTransport(String),
143
144    // ─── Identity / key bootstrap ───────────────────────────────────────
145    #[error("secret key error: {0}")]
146    SecretKey(String),
147
148    #[error("endpoint ID derivation failed: {0}")]
149    EndpointId(String),
150
151    // ─── Config ─────────────────────────────────────────────────────────
152    #[cfg(feature = "config")]
153    #[error("config error: {0}")]
154    Config(String),
155
156    // ─── Secrets bundle ──────────────────────────────────────────────────
157    #[cfg(feature = "config")]
158    #[error("secrets error: {0}")]
159    Secrets(String),
160
161    // ─── ACL ─────────────────────────────────────────────────────────────
162    #[cfg(feature = "acl")]
163    #[error("acl error: {0}")]
164    Acl(String),
165
166    // ─── Service registration ───────────────────────────────────────────
167    #[error("duplicate service ALPN: {0}")]
168    DuplicateService(String),
169
170    // ─── Generic pass-through ───────────────────────────────────────────
171    #[error(transparent)]
172    Other(#[from] anyhow::Error),
173}
174
175pub type Result<T> = std::result::Result<T, Error>;