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("broadcast messages must not be encrypted")]
58    BroadcastMustNotBeEncrypted,
59    #[error("message type requires an encrypted envelope")]
60    EncryptionRequired,
61    #[error("encrypted messages require a recipient")]
62    MessageRequiresRecipient,
63    #[error("context missing")]
64    EmptyContext,
65    #[error("invalid DID document context")]
66    InvalidContext,
67    #[error("controller missing")]
68    EmptyController,
69    #[error("verification method missing type")]
70    VerificationMethodMissingType,
71    #[error("invalid verification method type: {0}")]
72    InvalidVerificationMethodType(String),
73    #[error("unknown verification method: {0}")]
74    UnknownVerificationMethod(String),
75    #[error("public key multibase is empty")]
76    EmptyPublicKeyMultibase,
77    #[error("invalid public key multibase")]
78    InvalidPublicKeyMultibase,
79    #[error("invalid multicodec, expected {expected}, got {actual}")]
80    InvalidMulticodec { expected: u64, actual: u64 },
81    #[error("invalid key length, expected {expected}, got {actual}")]
82    InvalidKeyLength { expected: usize, actual: usize },
83    #[error("proof is missing")]
84    MissingProof,
85    #[error("invalid proof type: {0}")]
86    InvalidProofType(String),
87    #[error("invalid proof purpose: {0}")]
88    InvalidProofPurpose(String),
89    #[error("document signature is invalid")]
90    InvalidDocumentSignature,
91    #[error("message signature is invalid")]
92    InvalidMessageSignature,
93    #[error("invalid createdAt timestamp: {0}")]
94    InvalidCreatedAt(String),
95    #[error("invalid updatedAt timestamp: {0}")]
96    InvalidUpdatedAt(String),
97    #[error("missing envelope field: {0}")]
98    MissingEnvelopeField(&'static str),
99    #[error("invalid ephemeral key length")]
100    InvalidEphemeralKeyLength,
101    #[error("ciphertext too short")]
102    CiphertextTooShort,
103    #[error("cryptographic operation failed")]
104    Crypto,
105    #[error("CBOR encode failed: {0}")]
106    CborEncode(String),
107    #[error("CBOR decode failed: {0}")]
108    CborDecode(String),
109    #[error("JSON encode failed: {0}")]
110    JsonEncode(String),
111    #[error("JSON decode failed: {0}")]
112    JsonDecode(String),
113}
114
115// ─── Service-level errors ────────────────────────────────────────────────────
116
117/// Errors returned by ma-core public APIs.
118#[derive(Debug, Error)]
119pub enum Error {
120    // ─── Transport ──────────────────────────────────────────────────────
121    #[error("transport error: {0}")]
122    Transport(String),
123
124    #[error("transport connect failed: {0}")]
125    Connect(String),
126
127    #[error("transport bind failed: {0}")]
128    Bind(String),
129
130    #[error("stream open failed: {0}")]
131    StreamOpen(String),
132
133    #[error("connection closed: {0}")]
134    ConnectionClosed(String),
135
136    // ─── Validation ─────────────────────────────────────────────────────
137    #[error("message validation failed: {0}")]
138    Validation(#[from] MaError),
139
140    #[error("message signature verification failed")]
141    SignatureVerification,
142
143    #[error("replay detected for message {0}")]
144    Replay(String),
145
146    // ─── Resolution ─────────────────────────────────────────────────────
147    #[error("DID resolution failed for {did}: {detail}")]
148    Resolution { did: String, detail: String },
149
150    #[error("IPNS resolution failed for {path}: {detail}")]
151    IpnsResolution { path: String, detail: String },
152
153    #[error("no inbox transport in DID document for {0}")]
154    NoInboxTransport(String),
155
156    #[error("invalid transport string: {0}")]
157    InvalidTransport(String),
158
159    // ─── Identity / key bootstrap ───────────────────────────────────────
160    #[error("secret key error: {0}")]
161    SecretKey(String),
162
163    #[error("endpoint ID derivation failed: {0}")]
164    EndpointId(String),
165
166    // ─── Config ─────────────────────────────────────────────────────────
167    #[cfg(feature = "config")]
168    #[error("config error: {0}")]
169    Config(String),
170
171    // ─── Secrets bundle ──────────────────────────────────────────────────
172    #[cfg(feature = "config")]
173    #[error("secrets error: {0}")]
174    Secrets(String),
175
176    // ─── ACL ─────────────────────────────────────────────────────────────
177    #[cfg(feature = "acl")]
178    #[error("acl error: {0}")]
179    Acl(String),
180
181    // ─── Service registration ───────────────────────────────────────────
182    #[error("duplicate service ALPN: {0}")]
183    DuplicateService(String),
184
185    // ─── Generic pass-through ───────────────────────────────────────────
186    #[error(transparent)]
187    Other(#[from] anyhow::Error),
188}
189
190pub type Result<T> = std::result::Result<T, Error>;